Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ understanding RVO (as compared to returning local variable reference)

Tags:

c++

c++11

It's my first year of using C++ and learning on the way. I'm currently reading up on Return Value Optimizations (I use C++11 btw). E.g. here https://en.wikipedia.org/wiki/Return_value_optimization, and immediately these beginner examples with primitive types spring to mind:

int& func1()
{
    int i = 1;
    return i;
} 
//error, 'i' was declared with automatic storage (in practice on the stack(?)) 
//and is undefined by the time function returns 

...and this one:

int func1()
{
    int i = 1;
    return i;
}
//perfectly fine, 'i' is copied... (to previous stack frame... right?)

Now, I get to this and try to understand it in the light of the other two:

Simpleclass func1()
{
    return Simpleclass();
}

What actually happens here? I know most compilers will optimise this, what I am asking is not 'if' but:

  • how the optimisation works (the accepted response)
  • does it interfere with storage duration: stack/heap (Old: Is it basically random whether I've copied from stack or created on heap and moved (passed the reference)? Does it depend on created object size?)
  • is it not better to use, say, explicit std::move?
like image 799
DailyFrankPeter Avatar asked Mar 10 '23 03:03

DailyFrankPeter


1 Answers

You won't see any effect of RVO when returning ints.

However, when returning large objects like this:

struct Huge { ... };

Huge makeHuge() {
    Huge h { x, y, x };
    h.doSomething();
    return h;
}

The following code...

auto h = makeHuge();

... after RVO would be implemented something like this (pseudo code) ...

h_storage = allocate_from_stack(sizeof(Huge));
makeHuge(addressof(h_storage));
auto& h = *properly_aligned(h_storage);

... and makeHuge would compile to something like this...

void makeHuge(Huge* h_storage) // in fact this address can be
                               // inferred from the stack pointer
                               // (or just 'known' when inlining).
{
    phuge = operator (h_storage) new Huge(x, y, z);
    phuge->doSomething(); 
}
like image 121
Richard Hodges Avatar answered Apr 09 '23 00:04

Richard Hodges