10

Imagine such situation that I have a function like this:

Object f()
{
    Object obj;
    return obj;
}

Where sizeof(Object) is a big value.

And then I make a call of this function:

Object object = f();  

Do i understand correctly that first Object will be created on a stack (in the function) and then will be copied to object variable?

If so, is it reasonably to create an object in the function on a heap and to return a pointer to it instead of a copy ?

But i mean that the object must be created in the f() function - not passed by a pointer or a reference to this function and initialized.

EDIT

I don't mean that f is a very simple function. It can have a really complex routine of object initialization depending on some context. Will the compiler still optimize it as well?

Andrew
  • 24,218
  • 13
  • 61
  • 90
  • Technically yes. But for such a simple function the compiler will optimize away the copy. Build the code put print statements in the constructor/copy constructor and destructor then build the code without optimizations and with full optimizations and see how many print statements are executed. – Martin York Jan 26 '11 at 19:38
  • Well, if this is C, then it's just a call to malloc. If it is C++ then you'd new the object. Presumably your function does something to the object before returning it, otherwise it would serve no purpose. Why did you tag it C and C++? They are different. – David Heffernan Jan 26 '11 at 19:39
  • c and c++ because it maybe a struct or a class. So such situation can appear in both languages – Andrew Jan 26 '11 at 19:41
  • Are you wanting to create a class factory or just do some manipulation of a large object? – ThomasMcLeod Jan 26 '11 at 19:44
  • I want some object to be created in some context for example – Andrew Jan 26 '11 at 19:49

7 Answers7

19

For that specific case, you can take advantage of the fact that compilers nowadays are smart enough to optimize for it. The optimization is called named return value optimization (NRVO), so it's okay to return "big" objects like that. The compiler can see such opportunities (especially in something as simple as your code snippet) and generate the binary so that no copies are made.

You can also return unnamed temporaries:

Object f()
{
    return Object();
}

This invokes (unnamed) return value optimization (RVO) on just about all modern C++ compilers. In fact, Visual C++ implements this particular optimization even if all optimizations are turned off.

These kinds of optimizations are specifically allowed by the C++ standard:

ISO 14882:2003 C++ Standard, §12.8 para. 15: Copying Class Objects

When certain criteria are met, an implementation is allowed to omit the copy construction of a class object, even if the copy constructor and/or destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy operation as simply two different ways of referring to the same object, and the destruction of that object occurs later of the times when the two objects would have been destroyed without the optimization. This elison of copy operations is permitted in the following circumstances (which may be combined to eliminate multiple copies):

  • in a return statement in a function with a class terturn type, when the expression is the name of a non-volatile automatic object with the same cv-unqualified type as the function return type, the copy operation can be omitted by constructing the automatic object directly into the function's return value
  • when a temporary class object that has not been bound to a reference would be copied to a class object with the same cv-unqualitied type, the copy operation can be omitted by constructing the temporary object directly into the target of the omitted copy.

Generally, the compiler will always try to implement NRVO and/or RVO, although it may fail to do so in certain circumstances, like multiple return paths. Nevertheless, it's a very useful optimization, and you shouldn't be afraid to use it.

If in doubt, you can always test your compiler by inserting "debugging statements" and see for yourself:

class Foo
{
public:
    Foo()                      { ::printf("default constructor\n"); }
    // "Rule of 3" for copyable objects
    ~Foo()                     { ::printf("destructor\n");          }
    Foo(const Foo&)            { ::printf("copy constructor\n");    }
    Foo& operator=(const Foo&) { ::printf("copy assignment\n");     } 
};

Foo getFoo()
{
    return Foo();
}

int main()
{
    Foo f = getFoo();
}

If the returned object isn't meant to be copyable, or (N)RVO fails (which is probably not likely to happen), then you can try returning a proxy object:

struct ObjectProxy
{
private:
    ObjectProxy() {}
    friend class Object;    // Allow Object class to grab the resource.
    friend ObjectProxy f(); // Only f() can create instances of this class.
};

class Object
{
public:
    Object() { ::printf("default constructor\n"); }
    ~Object() { ::printf("destructor\n"); }
    // copy functions undefined to prevent copies
    Object(const Object&);
    Object& operator=(const Object&);
    // but we can accept a proxy
    Object(const ObjectProxy&)
    {
        ::printf("proxy constructor\n");
        // Grab resource from the ObjectProxy.
    }
};

ObjectProxy f()
{
    // Acquire large/complex resource like files
    // and store a reference to it in ObjectProxy.
    return ObjectProxy();
}

int main()
{
     Object o = f();
}

Of course, this isn't exactly obvious so proper documentation would be needed (at least a comment about it).

You can also return a smart pointer of some kind (like std::auto_ptr or boost::shared_ptr or something similar) to an object allocated on the free-store. This is needed if you need to return instances of derived types:

class Base {};
class Derived : public Base {};

// or boost::shared_ptr or any other smart pointer
std::auto_ptr<Base> f()
{
    return std::auto_ptr<Base>(new Derived);
}
In silico
  • 51,091
  • 10
  • 150
  • 143
  • great answer, what do you think of bringing C++0x to the mix (move semantics and `unique_ptr`) ? – Matthieu M. Jan 27 '11 at 07:15
  • @Matthieu M. C++0x's move semantics will basically eliminate the need for the `ObjectProxy` method and `std::unique_ptr<>` will replace `std::auto_ptr<>`. In short, it will be an improvement. (N)RVO will still be relevant. – In silico Jan 27 '11 at 07:17
  • I know, I was just wondering if it was worth extending an already long answer with these details. – Matthieu M. Jan 27 '11 at 09:05
  • Check this http://stackoverflow.com/questions/35506708/move-constructor-vs-copy-elision-which-one-gets-called why in the first case NRVO doesn't apply? – gedamial Feb 20 '16 at 12:24
2

In theory what you describe is what should happen. Anyway compilers are often able to optimize it in a way, that the caller's Object is used: f will directly write on caller's object and return null.

This is called Return Value Optimization (or RVO)

peoro
  • 25,562
  • 20
  • 98
  • 150
2

Do i understand correctly that first Object will be created on a stack (in the function) and then will be copied to object variable?

Yes obj is created on the stack but when you return a process called return value optimisation or RVO can prevent the unnecessary copy.

If so, is it reasonably to create an object in the function on a heap and to return a pointer to it instead of a copy ?

Yes it is reasonable to create an object on the heap and return a pointer to it as long as you clearly document the client is responsible for cleaning up the memory.

However, it's better than reasonable to return a smart pointer such as shared_ptr<Object> which alleviates the client from having to remember to explicitly free the memory.

Peter McG
  • 18,857
  • 8
  • 45
  • 53
  • Sometimes better than documentation is to use an appropriate smart pointer. – greyfade Jan 26 '11 at 20:06
  • In my opinion, you shouldn't leave the client responsible of anything, unless you want your email inbox full of "why is your library crashing all the time". Returning a smart pointer in this case is not even a matter of preference, it is mandatory IMO. Or, as I suggested, just pass-by-reference the object that will store the result. – Mikael Persson Jan 26 '11 at 20:42
2

The compiler will optimize it.

Except in some situations, such as:

std::string f(bool cond = false)
{
  std::string first("first");
  std::string second("second");
  // the function may return one of two named objects
  // depending on its argument. RVO might not be applied
  if(cond)
    return first;
  else
    return second;
}

Of course there can be some old compilers, which can call copy constructor. But you shouldn't worry about it with modern compilers.

Fred Nurk
  • 13,952
  • 4
  • 37
  • 63
UmmaGumma
  • 5,633
  • 1
  • 31
  • 45
  • Thanks man! http://stackoverflow.com/questions/35506708/move-constructor-vs-copy-elision-which-one-gets-called/35523387#35523387 – gedamial Feb 20 '16 at 12:40
2

Whether the compiler can apply RVO depends on the actual code involved. A general guideline is to create the returned value as late as possible. For example:

std::string no_rvo(bool b) {
  std::string t = "true", f = "fals";

  f += t[3];  // Imagine a "sufficiently smart compiler" couldn't delay initialization
  // for some reason, such not noticing only one object is required depending on some
  // condition.

  //return (b ? t : f);  // or more verbosely:
  if (b) {
    return t;
  }
  return f;
}

std::string probably_rvo(bool b) {
  // Delay creation until the last possible moment; RVO still applies even though
  // this is superficially similar to no_rvo.
  if (b) {
    return "true";
  }
  return "false";
}

With C++0x, the compiler is free to make even more assumptions, principally by being able to use move semantics. How those work is a 'nother can of worms, but move semantics are being designed so that they can apply to the exact code above. This helps most dramatically in the no_rvo case, but it provides guaranteed semantics in both cases, as a move operation (if possible) is preferred over a copy operation, while RVO is completely optional and not easy to check.

Fred Nurk
  • 13,952
  • 4
  • 37
  • 63
1

If your function f is a factory method, it is better to return a pointer, or an initialized smart pointer object such as auto_ptr.

auto_ptr<Object> f()
{
     return auto_ptr<Object>(new Object);
}

To use:

{    
    auto_ptr<Object> myObjPtr = f();
    //use myObjPtr . . . 
} // the new Object is deleted when myObjPtr goes out of scope
ThomasMcLeod
  • 7,603
  • 4
  • 42
  • 80
1

I don't know why nobody pointed out the obvious solution yet. Just pass the output object by reference:

void f(Object& result) {
  result.do_something();
  result.fill_with_values(/* */);
};

This way:

  • you avoid the copy for sure.

  • you avoid using the heap.

  • you avoid leaving the calling code with the responsibility of freeing the dynamically-allocated object (although shared_ptr or unique_ptr would do that too).

Another alternative is to make the function a member of Object, but that might not be appropriate, depending on what f()'s contract is.

Mikael Persson
  • 18,174
  • 6
  • 36
  • 52
  • A good solution. But it will not work for some times. For example if a f() is a method of some class which is the only method to create object (because only this class know the context of creation and have access to a private constructor of the object is created). – Andrew Jan 26 '11 at 20:56