简体   繁体   中英

different memory allocation of pointers in a code

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

输出代码 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

输出 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)

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM