[ACCEPTED]-Scope and return values in C++-scope

Accepted answer
Score: 53

When the function terminates, the following 13 steps happen:

  • The function’s return value 12 is copied into the placeholder that was put 11 on the stack for this purpose.

  • Everything 10 after the stack frame pointer is popped 9 off. This destroys all local variables 8 and arguments.

  • The return value is popped 7 off the stack and is assigned as the value of 6 the function. If the value of the function 5 isn’t assigned to anything, no assignment 4 takes place, and the value is lost.

  • The 3 address of the next instruction to execute 2 is popped off the stack, and the CPU resumes 1 execution at that instruction.

The stack and the heap

Score: 4

When you return a value, a copy is made. The 5 scope of the local variable ends, but a 4 copy is made, and returned to the calling 3 function. Example:

int funcB() {
  int j = 12;
  return j;
}

void A() {
  int i;
  i = funcB();
}

The value of j (12) is 2 copied and returned to i so that i will 1 receive the value of 12.

Score: 4

It really depends on what kind of variable 12 you are returning. If you return a primitive 11 then it is returned by copy, not by reference 10 so the value is copied to the top of the 9 stack (or, more often placed into a register) where 8 the calling function can get it. If you 7 allocate an object or memory on the heap 6 and return a pointer then it doesn't die 5 because it's on the heap, not the stack. If 4 you allocate something on the stack, however, and 3 return it, that would be a bad thing. For 2 instance, either of these would be very 1 bad:

int *myBadAddingFunction(int a, int b)
{
    int result;

    result = a + b;
    return &result; // this is very bad and the result is undefined
}

char *myOtherBadFunction()
{
    char myString[256];

    strcpy(myString, "This is my string!");
    return myString; // also allocated on the stack, also bad
}
Score: 3

Just for a little bit more of a memory-model 17 oriented explanation: when a function is 16 called, a temporary space is made for the 15 function to put its local variables, called 14 a frame. When the function (callee) returns its 13 value, it puts the return value in the frame 12 of the function that called it (caller), and 11 then the callee frame is destroyed.

The "frame 10 is destroyed" part is why you can't return 9 pointers or references to local variables 8 from functions. A pointer is effectively 7 a memory location, so returning the memory 6 location of a local variable (by definition: a 5 variable within the frame) becomes incorrect 4 once the frame is destroyed. Since the callee 3 frame is destroyed as soon as it returns 2 its value, any pointer or reference to a 1 local variable is immediately incorrect.

Score: 3

This depends on the type of the returned 15 item. If you are returning by value, a 14 new copy of the variable is made to return 13 to the caller. I thins case you do not 12 need to worry about object lifetime, but 11 you may need to worry about the costs of 10 copying objects (but please don't prematurely 9 optimze - correctness is much more important):

std::string someFunc( std::string& const s)
{
    return s + "copy";
}

If 8 the function is returning a reference, then 7 you need to be careful about what you're 6 returning because it's lifetime needs to 5 extend beyond the function's lifetime and 4 the caller will not necessarily be able 3 todelete it if you're using new to create the object:

std::string& someFunc2( std::string const& s)
{
    return s + "reference to a copy";   // this is bad - the temp object created will 
                                        //  be destroyed after the expression the 
                                        //  function call is in finishes.
                                        //  Some, but not all, compilers will warn 
                                        //  about this.
}

Of 2 course, returning pointers will have similar 1 lifetime considerations.

Score: 2

The local variable is copied to the return 4 value. Copy constructors are called for 3 non-trivial classes.

If you return a pointer 2 or reference to a local variable you will 1 have trouble---just as your intuition suggested.

More Related questions