Click here to Skip to main content
15,886,919 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to free the variable Pin
Aescleal12-Feb-11 5:19
Aescleal12-Feb-11 5:19 
GeneralRe: How to free the variable Pin
goldenrose912-Feb-11 6:23
goldenrose912-Feb-11 6:23 
GeneralRe: How to free the variable Pin
Aescleal12-Feb-11 10:07
Aescleal12-Feb-11 10:07 
AnswerRe: How to free the variable PinPopular
Aescleal12-Feb-11 5:17
Aescleal12-Feb-11 5:17 
GeneralRe: How to free the variable [modified] Pin
Niklas L12-Feb-11 6:30
Niklas L12-Feb-11 6:30 
GeneralRe: How to free the variable [modified] Pin
goldenrose912-Feb-11 7:30
goldenrose912-Feb-11 7:30 
GeneralRe: How to free the variable Pin
Niklas L12-Feb-11 8:18
Niklas L12-Feb-11 8:18 
GeneralRe: How to free the variable PinPopular
Aescleal12-Feb-11 10:02
Aescleal12-Feb-11 10:02 
I'd have said that was true 10 years ago, but these days compilers have made that sort of trick fairly pointless. When compilers see a function of the form:

A some_function()
{
    A a;

    /* Other stuff */

    return a;
}


they're allowed (by the standard) to rewrite it as:

void some_function( memory_block_the_size_of_a &a )
{
    new( &a ) A;

    /* Other stuff */
}


The compiler then converts the calls to that function from:

A a = some_function();


to something like:

memory_block_the_size_of_a block;
A &a( *reinterpret_cast<A *>( block ) );
some_function( &block );

/* Other stuff */

a->~A();


which removes the copy construction which would normally happen with this sort of construct. The transformation the compiler does is a bit hard to represent in C++ as what it produces is usually exception safe while the representation I've done above isn't. That's the beauty of being a compiler I suppose!

This transformation is called NRVO (named return value optimisation). It's fairly unique in that it's one of the few transformations the compiler may or may not do to some code which change it's visible behaviour. NRVO was implemented in VC++ 2005 and at least gcc 4.0, but it might have been a version or two earlier, can't remember without checking.

Anyway, while you can use a reference parameter it (in my opinion at least) makes your code a lot more stilted as you end up writing two lines where you only needed one which was a direct statement of what you were trying to achieve. If you use the double ended reference type of function then you can actually end up with slower code if someone writes (naively):

A a = some_function( b );


as you still end up triggering the copy constructor. So the moral here is either provide an in/out parameter or return by value - don't fall halfway between.

Lest anyone gets a bit slap happy and starts changing large swathes of code to this style it's worth noting that the compiler can't apply NRVO if:

- The thing being returned isn't named (that's the named bit...)
- There are multiple exits from the function, even if they're all returning the same thing
- It's assignment not copy construction (so A a = some_function() can invoke NRVO while a = some_function() won't).

In addition vendors don't have to implement it but most do - if they didn't they'd just make their compilers seem bad.

While I'm banging on I should mention there's a similar optimisation for functions with a single return of an unamed temporary object called RVO. I'll only bother talking about it if there's any interest though!

Cheers,

Ash
GeneralRe: How to free the variable Pin
Niklas L12-Feb-11 12:56
Niklas L12-Feb-11 12:56 
GeneralRe: How to free the variable Pin
Stefan_Lang13-Feb-11 21:58
Stefan_Lang13-Feb-11 21:58 
GeneralRe: How to free the variable Pin
Niklas L13-Feb-11 23:28
Niklas L13-Feb-11 23:28 
GeneralRe: How to free the variable Pin
Stefan_Lang14-Feb-11 0:18
Stefan_Lang14-Feb-11 0:18 
GeneralRe: How to free the variable Pin
Aescleal12-Feb-11 10:10
Aescleal12-Feb-11 10:10 
QuestionMessage to response function SetFont() Pin
includeh1012-Feb-11 3:38
includeh1012-Feb-11 3:38 
AnswerRe: Message to response function SetFont() Pin
Code-o-mat12-Feb-11 6:52
Code-o-mat12-Feb-11 6:52 
AnswerRe: Message to response function SetFont() Pin
User 742933813-Feb-11 2:57
professionalUser 742933813-Feb-11 2:57 
AnswerRe: Message to response function SetFont() Pin
Hans Dietrich12-Feb-11 6:53
mentorHans Dietrich12-Feb-11 6:53 
QuestionCEdit Box Error Pin
raju_shiva12-Feb-11 0:40
raju_shiva12-Feb-11 0:40 
AnswerRe: CEdit Box Error Pin
Richard MacCutchan12-Feb-11 2:04
mveRichard MacCutchan12-Feb-11 2:04 
QuestionUsing gdiplus to skew, rotate and resize a matrix (image) Pin
Kiran Satish11-Feb-11 13:43
Kiran Satish11-Feb-11 13:43 
AnswerRe: Using gdiplus to skew, rotate and resize a matrix (image) Pin
Rozis14-Feb-11 10:34
Rozis14-Feb-11 10:34 
QuestionNew Company Pin
VonHagNDaz11-Feb-11 3:27
VonHagNDaz11-Feb-11 3:27 
AnswerRe: New Company Pin
Rajesh R Subramanian11-Feb-11 3:42
professionalRajesh R Subramanian11-Feb-11 3:42 
GeneralRe: New Company Pin
VonHagNDaz11-Feb-11 3:44
VonHagNDaz11-Feb-11 3:44 
QuestionHow to increase buffer size for CEdit Box in VC6 Pin
raju_shiva10-Feb-11 22:53
raju_shiva10-Feb-11 22:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.