代码中指针的不同内存分配

发布于 2025-01-23 07:51:42 字数 2840 浏览 0 评论 0原文

为什么这两个代码在其糊状的内存位置之间存在不同的内存空间差异?

查看函数_1并在代码1和2中输出,以更好地理解问题。

代码1

#include <iostream>
using namespace std;

static int *function_1()
{
static int *pointer_1;// main point to be looked at in code 1
static int variable_1 = 77;
pointer_1 = &variable_1;
return pointer_1;
}
static int *function_2()
{
static int variable_2 = 777;
static int *pointer_2 = &variable_2;
return pointer_2;
}
static int *function_3()
{
static int variable_3 = 77;
static int *pointer_3 = &variable_3;
return pointer_3;
}
int main()
{
cout << "function - 1 referenced = " << function_1() << "  derefered = " << * 
(function_1()) << " fuc1 size =" << sizeof(function_1()) << endl;
cout << "function - 2 referenced = " << function_2() << "  derefered = " << * 
(function_2()) << " fuc2 size =" << sizeof(function_2()) << endl;
cout << "function - 3 referenced = " << function_3() << "  derefered = " << * 
(function_3()) << " fuc2 size =" << sizeof(function_2()) << endl;
return 0;
}

输出-1

“输出代码1”

code 2
#include <iostream>
using namespace std;

static int *function_1()
{
static int variable_1 = 77;
static int* pointer_1 = &variable_1; //main point to be looked at code 2 
return pointer_1;
}
static int *function_2()
{
static int variable_2 = 777;
static int *pointer_2 = &variable_2;
return pointer_2;
}
static int *function_3()
{
static int variable_3 = 77;
static int *pointer_3 = &variable_3;
return pointer_3;
}
int main()
{
cout << "function - 1 referenced = " << function_1() << "  derefred = " << * 
(function_1()) << " fuc1 size =" << sizeof(function_1()) << endl;
cout << "function - 2 referenced = " << function_2() << "  derefred = " << * 
(function_2()) << " fuc2 size =" << sizeof(function_2()) << endl;
cout << "function - 3 referenced = " << function_3() << "  derefred = " << * 
(function_3()) << " fuc2 size =" << sizeof(function_2()) << endl;
return 0;
}

输出-2

“ alt = ”

输出

i.sstatic.net/g1fn7.png

2 “

> 4206612 //主要点

0x403020-4206624

在代码2中,我们有输出

hexaddress-转换后的十进制地址

0x403010-4206608

0x403020-4206624 //主要点

0x4030303030-40x4030-4206640

为什么在继续存在如此差异的情况下,只是在继续存在的位置,并且因为宣传而宣传,并且在宣传,并且最初是宣传,并且最初是宣传的,又是宣传的,一个步骤(代码2)和2步(代码1)

why is there different difference of memory space between these two codes in their contigious memory locations ?

look at function_1 and output in both code 1 and 2 for better understanding of the problem.

code 1

#include <iostream>
using namespace std;

static int *function_1()
{
static int *pointer_1;// main point to be looked at in code 1
static int variable_1 = 77;
pointer_1 = &variable_1;
return pointer_1;
}
static int *function_2()
{
static int variable_2 = 777;
static int *pointer_2 = &variable_2;
return pointer_2;
}
static int *function_3()
{
static int variable_3 = 77;
static int *pointer_3 = &variable_3;
return pointer_3;
}
int main()
{
cout << "function - 1 referenced = " << function_1() << "  derefered = " << * 
(function_1()) << " fuc1 size =" << sizeof(function_1()) << endl;
cout << "function - 2 referenced = " << function_2() << "  derefered = " << * 
(function_2()) << " fuc2 size =" << sizeof(function_2()) << endl;
cout << "function - 3 referenced = " << function_3() << "  derefered = " << * 
(function_3()) << " fuc2 size =" << sizeof(function_2()) << endl;
return 0;
}

output - 1

OUTPUT CODE 1

code 2
#include <iostream>
using namespace std;

static int *function_1()
{
static int variable_1 = 77;
static int* pointer_1 = &variable_1; //main point to be looked at code 2 
return pointer_1;
}
static int *function_2()
{
static int variable_2 = 777;
static int *pointer_2 = &variable_2;
return pointer_2;
}
static int *function_3()
{
static int variable_3 = 77;
static int *pointer_3 = &variable_3;
return pointer_3;
}
int main()
{
cout << "function - 1 referenced = " << function_1() << "  derefred = " << * 
(function_1()) << " fuc1 size =" << sizeof(function_1()) << endl;
cout << "function - 2 referenced = " << function_2() << "  derefred = " << * 
(function_2()) << " fuc2 size =" << sizeof(function_2()) << endl;
cout << "function - 3 referenced = " << function_3() << "  derefred = " << * 
(function_3()) << " fuc2 size =" << sizeof(function_2()) << endl;
return 0;
}

output - 2

OUTPUT 2

now as we can see in output of code 1 and code 2

in code 1 we have output

hexaddress - converted decimal address

0x403010 - 4206608

0x403014 - 4206612 // main point

0x403020 - 4206624

in code 2 we have output

hexaddress - converted decimal address

0x403010 - 4206608

0x403020 - 4206624 //main point

0x403030 - 4206640

why is there so much difference in continious memory location just because of declaring and initializing of pointer vairable in one step (code 2) and in 2 step (code 1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潇烟暮雨 2025-01-30 07:51:42

根据输出,指针的大小等于8,类型int的对象的大小等于4

输出取决于如何在记忆中放置在功能中声明的静态存储持续时间的对象。

在第一个演示程序中,我们将

static int *pointer_1;// main point to be looked at in code 1
static int variable_1 = 77;
static int variable_2 = 777;
static int *pointer_2 = &variable_2;
static int variable_3 = 77;
static int *pointer_3 = &variable_3

Pointer Pointer_1指向变量变量_1,指针Pointer_2指向变量变量_2。 Between these two variables, variable_1 and variable_2, there is no gap due to possible alingment.因此,输出是

0x403010
0x403014

变量变量_2和变量_3之间的输出。结果,两个变量之间有12个字节(可变_2和8个字节占据的4个字节由PointerPointer_2。8 + 4 = 12占用)。因此,您的

0x403010
0x403014
0x403020

方式可以分析其他程序的输出,即由于变量后的指针,变量和指针之间可能存在差距。

例如,可以将Pointer_1之后放置的变量_2附加到4个字节上,以确保指针Pointer_2的对齐(使Pointer Pointer pointer_2可将内存的地址划分为8)

static int pointer_1 = &variable_1;
static int variable_2 = 777;
static int *pointer_2 = &variable_2;

According to the output the size of pointer is equal to 8 and the size of an object of the type int is equal to 4.

The output depends on how the objects with static storage duration declared in the functions are placed by the compiler in memory.

In the first demonstration program we have

static int *pointer_1;// main point to be looked at in code 1
static int variable_1 = 77;
static int variable_2 = 777;
static int *pointer_2 = &variable_2;
static int variable_3 = 77;
static int *pointer_3 = &variable_3

The pointer pointer_1 points to the variable variable_1 and the pointer pointer_2 points to the variable variable_2. Between these two variables, variable_1 and variable_2, there is no gap due to possible alingment. So the output is

0x403010
0x403014

But between the variables variable_2 and variable_3 there is the pointer pointer_2 that occupies 8 bytes. As a result between the two variable there are 12 bytes (4 bytes occupied by variable_2 and 8 bytes occupied by the pointer pointer_2. 8 + 4 = 12). So you have

0x403010
0x403014
0x403020

The same way you can analyze the output of the other programs taking into account that there can be gaps between variables and pointers due to alingment of pointers after variables.

For example the variable variable_2 placed after the pointer pointer_1 can be appended by 4 bytes to guarantee the alignment of the pointer pointer_2 (to make the address of the memory occupied by the pointer pointer_2 divisible by 8)

static int pointer_1 = &variable_1;
static int variable_2 = 777;
static int *pointer_2 = &variable_2;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文