How to avoid the overhead of copying when returning objects from functions?

To avoid the overhead of copying when passing objects into functions, there are references.

is there a similar construct for when we want to avoid copying when returning objects/containers from functions?

Or is the only solution to is to pass pointers around and use new inside the functions (this seems ugly)?
is there a similar construct for when we want to avoid copying when returning objects/containers from functions?

If the situation permits, you could pass the object by reference and have that function modify it. The function would now return void. Besides that, no, if you return an object from a function, there would be a copy on the caller's side. Fortunately, most (all?) popular compilers support return value optimization*, so the temporary object placed in the return value will not be created if you have optimizations turned on, situation permitting of course.

I am a firm believer of if there's no observed performance problem, then there's no performance problem. That is, if the overhead of copying objects on function returns isn't causing any noticeable slowdown, then there really isn't anything to worry about yet.

Or is the only solution to is to pass pointers around and use new inside the functions (this seems ugly)?

Your intuition is right: don't pass pointers around just to avoid copying overhead. It just tarnishes the legibility of the program.

*http://en.wikipedia.org/wiki/Return_value_optimization
Last edited on
I also read http://stackoverflow.com/questions/2025287/sending-back-a-vector-from-a-function
and http://stackoverflow.com/questions/4986673/c11-rvalues-and-move-semantics-confusion/6638485#6638485

So now compilers use move assignments which avoid the overhead of copying?
Is it done for all objects automatically, or do I have to define something for my own defined objects (as opposed to STL containers)?

Finally, is there a reference which explains the move construct?

EDIT: Found this:
http://akrzemi1.wordpress.com/2011/08/11/move-constructor/
Lots of things to keep in mind!!!!

It should be noted that copy elision, if it is employed, renders faster program than optimizations based on move construction. In the above example of vector’s move constructors we had to use six pointer assignments. In contrast, copy elision does not require a single instruction. However, copy elision is not guaranteed to always work. It may be because it is unavailable in your compiler, because some optimizations have been disabled to speed up the compilation or because in some particular cases it is not doable. The move-construction optimization, in contrast, is guaranteed to always work.


The programmer has to figure out if copy elison will work on not.
Now, to figure out when copy elison will work!!

pass the object by reference and have that function modify it
soln that shacktar mentioned is probably the best if it can be done.
Last edited on
Topic archived. No new replies allowed.