-
August 1st, 2011, 12:58 AM
#1
variables addreses
Please look at this code:
int main()
{
int a=5;
int b;
int *c=new int(5);
cout<<&a<<endl; //0012FED4
cout<<&b<<endl; //0012FEC8
cout<<&c<<endl; //0012FEBC
return 0;
}
As you can see there is 12 bytes space between every address.
Now look at this:
int a=5;
int b=8;
int c;
int *d=new int(5);
int main()
{
cout<<&a<<endl; //00456014
cout<<&b<<endl; //00456018
cout<<&c<<endl; //004576A0
cout<<&d<<endl; //004576A4
return 0;
}
Between Variable (a) and (b) is 4 bytes space but for (c) its different. There is approximately 6000 bytes space. What’s the point of this?
I’ve learnt that initialized variable is stored in .data section, uninitialized variables in .bss and pointers in heap. But here I am really confused. Why addresses in code one is contiguous with 12 bytes spaces. Why 12? If integer is 4 bytes! What is this extra 8 bytes?
And why uninitialized variable is close to a pointer variable that is supposed to be in heap section.
Why base address for variables inside main function is 0012FED4 but for second code its 00456014.
Please someone write me a line about this issue.
Thanks for any advance.
-
August 1st, 2011, 07:52 AM
#2
Re: variables addreses
Originally Posted by amigo
[...]
As you can see there is 12 bytes space between every address.
[...]
Why addresses in code one is contiguous with 12 bytes spaces. Why 12? If integer is 4 bytes! What is this extra 8 bytes?
You most probably did this test with a debug build. In this case, the extra bytes are guard bytes added to detect stray memory accesses. When I tested this with a release build, they all lined up neatly tight using four bytes each, though in a different sequence. (Tested with VC++ 2010.)
And why uninitialized variable is close to a pointer variable that is supposed to be in heap section.
The dynamically allocated (with new or malloc()) space the pointer points to is supposed to be located on the heap, not the pointer itself. In that respect the pointer is not any different from any other POD variable.
Why base address for variables inside main function is 0012FED4 but for second code its 00456014.
Non-static local variables, whether initialized or not, are located on the stack, which is not taken into account here (and, as pointed out, your assumption about the pointers is wrong):
I’ve learnt that initialized variable is stored in .data section, uninitialized variables in .bss and pointers in heap.
Thus it is natural for those addresses to be far apart, though not compelling.
The bottom line of all that is, though, that it's not our task to worry about those things, it's that of the folks who write the compilers and perhaps the standard libraries. Stuff like that is highly implementation-dependent and you can't safely make any assumptions about it.
Please use code tags when posting code.
Ah, and... Welcome to CodeGuru!
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
August 1st, 2011, 10:33 AM
#3
Re: variables addreses
You need to pair a delete with that new. Yes, it's a toy program. But the best way to instill the good habits is to practice them every time.
-
August 1st, 2011, 10:45 AM
#4
Re: variables addreses
Originally Posted by amigo
Please look at this code:
Code:
int *c=new int(5);
//..
cout<<&c<<endl; //0012FEBC
You are not printing the value of the pointer returned by new. You are printing the address of the pointer, not the pointer value itself.
So calling "new" was not even necessary to show you these results.
Regards,
Paul McKenzie
-
August 3rd, 2011, 01:26 AM
#5
Re: variables addreses
thank you guys.you are amazing.please forgive me if i didnt reply quickly.its becouse i dont have internet in home and i have to come to coffeenet .
i have read your answers .it is so helpful.
and thank you again for your welcoming.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|