9

In this example, why is it ok to return a stack variable? When t() returns, why is it not returning garbage, since the stack pointer has been incremented?

#include << string >>
#include << vector >>
#include << iostream >>

using namespace std;

class X{
public:

  X() { cout << "constructor" << endl; }

  ~X() { cout << "destructor" << endl; }

};

vector <X> t()
{
  cout << "t() start" << endl;

  vector<X> my_x;

  int i = 0;

  printf("t: %x %x %x\n", t, &my_x, &i);

  my\_x.push\_back(X()); my\_x.push\_back(X()); my\_x.push\_back(X());

  cout << "t() done" << endl;

  return my_x;
}

int main()
{

  cout << "main start" << endl;

  vector <X> g = t();


  printf("main: %x\n", &g);

  return 0;

}

output:

./a.out
main start
t() start
t: 8048984 bfeb66d0 bfeb667c
constructor
destructor
constructor
destructor
destructor
constructor
destructor
destructor
destructor
t() done
main: bfeb66d0
destructor
destructor
destructor
1
  • 2
    It's dangerous to return a reference to a variable on stack as you might end up with garbage. Otherwise, a copy will be constructed and handed to the caller. Jul 5, 2009 at 0:25

4 Answers 4

25

Basically when you return the stack variable my_x you would be calling the copy constructor to create a new copy of the variable. This is not true, in this case, thanks to the all mighty compiler.

The compiler uses a trick known as return by value optimization by making the variable my_x really being constructed in the place of memory assigned for g on the main method. This is why you see the same address bfeb66d0 being printed. This avoids memory allocation and copy construction.

Sometimes this is not at all possible due to the complexity of the code and then the compiler resets to the default behavior, creating a copy of the object.

0
5

Because parameters are passed by value. A copy is made. So what is being returned is not the value on the stack, but a copy of it.

2
  • Actually, a copy is not made thanks to named RVO, as smink says and as the tracing proves (the address of my_x in function t is equal to the address of g in function main). But yes, the result is otherwise as if a copy had been made. Jul 5, 2009 at 1:14
  • True, but even without that optimization, his code would be valid, because the value gets copied, so what is returned is not a reference to the no-longer-valid object declared in the function.
    – jalf
    Jul 5, 2009 at 11:28
0

Well a copy is returned, the only construct you can not return a copy of it is a static array. So you can not say this...

int[] retArray()
{
    int arr[101];

    return arr;
}
0

The compiler is optimized to handle returning "stack variables" without calling the copy constructor. The only thing you need to know about is that that memory allocation is in scope of both the function that allocated it on the stack and the function that the object is returned to.

It does NOT call the copy constructor nor does it allocate it twice.

There may be some special cases and of course it depends on the compiler -- for example, primitives may be copied -- but in general, objects allocated on the stack, don't get copied when being returned.

Example:

struct Test {

};

Test getTest() {
    Test t;
    std::cout << &t << std::endl;        // 0xbfeea75f
    return t;
}


int main(int argc, char *argv[])
{
    Test t = getTest();
    std::cout << &t << std::endl;       // also 0xbfeea75f
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.