variables addreses
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: variables addreses

  1. #1
    Join Date
    Aug 2011
    Posts
    2

    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.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,588

    Re: variables addreses

    Quote Originally Posted by amigo View Post
    [...]

    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.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    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.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,431

    Re: variables addreses

    Quote Originally Posted by amigo View Post
    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

  5. #5
    Join Date
    Aug 2011
    Posts
    2

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center