Jump to content
  • Advertisement
Sign in to follow this  
Sc4Freak

Returning a large object from a function

This topic is 3177 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If you want to return a large (or expensive to copy) object from a function, you can either return it directly:
std::vector<std::string> Generate();
Or return it through an out parameter:
void Generate(std::vector<std::string>& out);
The first relies on the compiler to perform RVO to elide the extra copies, which should make it just as performant as the second version. But RVO isn't exactly difficult to inadvertently defeat, causing a lot of copying overhead. So in general, which do you prefer any why?

Share this post


Link to post
Share on other sites
Advertisement
I pretty much always use the out parameter method for non-primitive types... C++ doesn't have multiple return values anyway, so the out parameters wouldn't be completely avoidable regardless.

Share this post


Link to post
Share on other sites
Through an out parameter. Not because of any
technical reasons but rather that the return
value can be used for return/error codes.

I like to be as consistent as possible when
I have choice so I do it that way as often
as possible even when return codes aren't
very meaningful for that particular function.

Share this post


Link to post
Share on other sites
I usually return large objects from factory functions by auto_ptr. It's actually quite rare that a profiler tells me that this is a mistake.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
I usually return large objects from factory functions by auto_ptr. It's actually quite rare that a profiler tells me that this is a mistake.


This.

I use this idiom quite heavily and it almost always ends up being acceptable when it comes time to start profiling code. It also reinforces the idea that dynamically allocated objects should be handled by smart pointers rather than raw pointers/references as much as possible. Last but not least, it avoids having to deal with deep-copy issues if you use by-value returns; in my experience the real killer isn't objects getting copy constructed per se - it's usually the classes with really nasty deep-copy semantics that end up being the problem.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sc4Freak
The first relies on the compiler to perform RVO to elide the extra copies

And in the rare cases where RVO doesn't kick in, you still have move semantics as a safety net. All of the standard library containers are going to have move constructors in C++0x.

Quote:
Original post by Beyond_Repair
C++ doesn't have multiple return values anyway

You can return a tuple if you need more than one return value.

Quote:
Original post by Pzc
Through an out parameter. Not because of any technical reasons but rather that the return value can be used for return/error codes.

C++ has a dedicated language mechanism for signaling errors.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!