Results 1 to 4 of 4

Thread: Compiler's calculation on memory/address

  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Compiler's calculation on memory/address

    Hello All,
    How is an address being calculated by the compiler? or what does the compiler do internally when I write the following:
    For example:
    When I say
    Qt Code:
    1. int a;
    To copy to clipboard, switch view to plain text mode 
    some memory is allocated with some address by the compiler. My question is how?
    As per my understanding, the compiler takes the sizeof datatype which is "int" in above case and then does some manipulation to calculate the address which is a part of memory?

    Can someone please clear me about this?

    Thanks in advance.

  2. #2
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Compiler's calculation on memory/address

    You're allocating an int as a local variable on the stack. Take a look at this. For each variable (local and parameter) the compiler knows the offset from the frame pointer.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  3. #3
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Compiler's calculation on memory/address

    Quote Originally Posted by Michiel View Post
    You're allocating an int as a local variable on the stack. Take a look at this. For each variable (local and parameter) the compiler knows the offset from the frame pointer.
    The link tells only about the call stack. My question was how does the compiler calculate the address of a variable? I mean,

    For example:
    Qt Code:
    1. int main()
    2. {
    3. int a = 10;
    4. cout<<&a<<endl;
    5. }
    To copy to clipboard, switch view to plain text mode 
    On my system the address is 0012FED4.
    I want to know how is the address "0012FED4" calculated by the compiler? Any idea...

    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Compiler's calculation on memory/address

    It's exactly as Michiel said. "a" is placed as the first (or second, or third) variable on the stack. There is a register that holds the current frame pointer that points to the beginning of the stack frame. The frame contains things like the return address and the previous frame pointer. After that local variables (your "a" included) are placed. So the compiler knows, that to reference "a" it has to go to the address pointed by "FP" (frame pointer) and substract (or add, depending on the architecture) for instance "12". Every time you reference "a", the compiler changes that to "$FP-12".

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.