References

This is a discussion on References within the C++ Programming forums, part of the General Programming Boards category; I'm looking a lot more closely at referenes now, and I would like to know if there is a tutorial ...

  1. #1
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949

    References

    I'm looking a lot more closely at referenes now, and I would like to know if there is a tutorial on them. Why would you not return a reference whenever you return an object? Thanks !
    Do not make direct eye contact with me.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,643
    You wouldn't want to return a reference to an object on your stack.

    gg

  3. #3
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Your object would be destroyed within the function once the function completes. Unless of course its a static object.

  4. #4
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Duh, duh. What about functions that are always going to be used like this:
    var = rFunc();
    ? Thanks again !
    Do not make direct eye contact with me.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,643
    rFunc() can return a reference if it wants to - just make sure the memory that belongs to that reference is still valid after rFunc() returns.
    So you wouldn't want to return something off of rFunc()'s stack since that memory isn't valid once rFunc() returns.

    gg

  6. #6
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Your saying to make sure that the variable the reference is "aiming at" hasn't been destroyed? I suppose that makes sense .
    Do not make direct eye contact with me.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Correct. Do not use references for variables that are on the function's local stack.

    When you enter a function C sets up a stack. If you have parameters those are popped off of the stack and used. If you declare a variable inside of the function and attempt to return a reference to that variable, you will have major problems

    Code:
    int MyFunc(void)
    {
      int i=0;
      return i;
    }
    This is ok because the value of i is returned, not the address of i.

    What MyFunc might look like in assembly (NASM):
    Code:
    MyFunc:
      push     ebp                  //save base pointer
      mov      esp,ebp           //set stack pointer to base
      
      sub      esp,4               //subtract 4 for int -> int i=0;
      mov     [esp-4],0         //set i to 0, i is at esp-4
      
      mov     eax,[esp-4]     //mov value of 0 to esp-4 or i to return value to C
      
      add     esp,4               //Erase i by adding 4 to stack pointer
      pop     ebp                  //Restore base pointer
      ret                              //return to caller
    So if you were to return a reference to i, it no longer exists. But if you return the value of i, the compiler moves the value at [esp-4] or where 'i' is into eax - in C all integral returns are returned in EAX and all floating point return values are in ST(0).

    So you see the add esp,4 and pop ebp effectively erase i and its address. It no longer exists. Future functions will use the stack and alter what we called 'i'.

    Stack on entry to MyFunc

    ---------------> top of stack on entry
    previous value of EBP
    caller address
    function parameters


    Then when we declare integer 'i' which takes 4 bytes in 32-bit code - we reserve 4 bytes above the current stack pointer for this:

    Stack after declaring variable 'i'

    [esp-4] 'i' -----> 4 bytes above the top of the stack
    -----------------> top of stack on entry
    previous value of EBP
    caller address
    function parameters
    ....
    ....

    And this on exit from MyFunc (after the parameters are popped off of the stack or removed):

    Stack on exit from MyFunc
    (previous position of variable i)
    ----------------> top of stack on exit
    previous value of EBP
    caller address
    (parameters -> are popped off before returning to caller)


    So if you return a reference to where i was, you will have major issues in your code because 'i' does not exist anymore.

    Also you will note that the pop ebp will pop the first or top item off of the stack which just happens to be the previous address of EBP - so EBP is restored to what it was on entry. Then the RET will return to the caller - essentially - the actual nitty gritty of the RET instruction takes about 3 pages in the Intel manuals.
    Last edited by VirtualAce; 12-23-2003 at 12:25 AM.

  8. #8
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Got it, thanks for the VERY extended explanation . I don't know NASM, but I do understand in a psudocode sense, thanks all .
    Do not make direct eye contact with me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler settings, references etc...
    By ejohns85 in forum C++ Programming
    Replies: 0
    Last Post: 05-14-2009, 04:53 AM
  2. VC++ 2005 Express: missing references page
    By psychopath in forum Tech Board
    Replies: 1
    Last Post: 08-21-2006, 04:55 PM
  3. Vector of references
    By roktsyntst in forum C++ Programming
    Replies: 5
    Last Post: 04-15-2003, 06:40 PM
  4. declare references to references works!
    By ManuelH in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2003, 07:14 AM
  5. Pointers and references...
    By SushiFugu in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 03:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21