Take the 2-minute tour ×

Possible Duplicate:
Understanding return value optimization and returning temporaries - C++

let Integer be some class with i as it's member.left and right are passed as arguments to a function call and are of type Integer

Now as given in Bruce Eckel.

Code 1:

return Integer(left.i+right.i);

Code 2:

Integer tmp(left.i+right.i);
return tmp;

code 1 says make a temporary integer object and return it and it is different from creating a named local variable and returning it,which is a general misconception.

In Code 1(called as returning a temporary approach):
The compiler knows that you have no other need for the object it's creating then to return it.The compiler takes advantage of this by building the object directly into the location of the outside return value. This requires only a single ordinary constructor call(no copy-constructor) and no destructor is required as no local object was created.

While 3 things will happen in code 2:
a) tmp object is created including its constructor call
b) the copy-constructor copies the tmp to the location of the outside return value.
c) the destructor is called for tmp at the end of scope.

In code 1 what does this mean : building the object directly into the location of the outside return value ?
also why copy constructor will not be called in code 1 ?

Also I didn't understand what does step b in code 2 is doing ? i.e.the copy-constructor copies the tmp to the location of the outside return value.

share|improve this question

marked as duplicate by Mat, littleadv, Bo Persson, Nicol Bolas, iammilind Jan 4 '12 at 9:11


Integer belongs to Java. –  iammilind Jan 4 '12 at 8:25
Assume it is defined here(code not shown) and it has one int member i and rest functions. –  Amol Sharma Jan 4 '12 at 8:26
A good compiler would create the same assembly code for both the cases. –  littleadv Jan 4 '12 at 8:27
plz explain it for a general Compiler.. –  Amol Sharma Jan 4 '12 at 8:29
@AmolSharma: that sentence just means a compiler that doesn't do NRVO will create an ordinary variable on stack, and will have to copy that temporary into the caller's object. Please read the article linked in the answer to that other question (msdn.microsoft.com/en-us/library/ms364057(v=vs.80).aspx), compilers know how to avoid it these days. –  Mat Jan 4 '12 at 8:38
show 5 more comments

1 Answer

The compiler is free to optimize out copy constructors for pass-by-value and return-by-value. My guess is that any decent optimizer will generate the same binary for both variants.

An optimized version of the code:

A foo()
   A temp;
00401000  mov         ecx,dword ptr [A::x (40337Ch)] 
00401006  mov         dword ptr [eax],ecx 
00401008  add         ecx,1 
0040100B  mov         dword ptr [A::x (40337Ch)],ecx 
   return temp;

So you see, the copy constructor is not called, even though, in my version, it has a cout, so it does influence observed behavior.

Without optimization:

A foo()
004113C0  push        ebp  
004113C1  mov         ebp,esp 
004113C3  sub         esp,0CCh 
004113C9  push        ebx  
004113CA  push        esi  
004113CB  push        edi  
004113CC  lea         edi,[ebp-0CCh] 
004113D2  mov         ecx,33h 
004113D7  mov         eax,0CCCCCCCCh 
004113DC  rep stos    dword ptr es:[edi] 
   A temp;
004113DE  lea         ecx,[temp] 
004113E1  call        A::A (4110E6h) 
   return temp;
004113E6  lea         eax,[temp] 
004113E9  push        eax  
004113EA  mov         ecx,dword ptr [ebp+8] 

/// copy constructor called on next line:

004113ED  call        A::A (411177h) 
004113F2  mov         eax,dword ptr [ebp+8] 

The phrase "b) the copy-constructor copies the tmp to the location of the outside return value." is far-feched, sometimes even that doesn't happen.

What you should remember from this is that you shouldn't rely on copy-constructors being called.

share|improve this answer
Perfectly reasonable answer, don't know why it was downvoted. –  littleadv Jan 4 '12 at 8:34
@littleadv it probably didn't answer the question completely. Regardless, I edited to be a little more explanatory. –  Luchian Grigore Jan 4 '12 at 8:39
add comment
