views:

609

answers:

4

How do people normally manage copying a list of large objects around?

Here's my situation:

Currently I have this:

typedef std::vector<float> Image;

and I'm storing it in a

std::list<Image> lst;

The Image.size() is quite large (each is ~3-5 MB).

I'm passing (copying) the list around.

Is it a correct understanding on my part that std::vector will copy each element by value? If so, the performance might be a bit awful due to excessive copying?

What can I do to minimize copying? Should I instead store

std::list<ImageRef> lst;

where

typedef boost::shared_ptr<Image>   ImageRef;

?

What's the elegant way of handling this kind of issue?

+4  A: 

Objects larger than built-in types are most often way cheaper to pass around by reference then by value. So if your object is about 3 Meg big, and you need to pass it around, please don't copy it!

All STL types use value semantics: they copy their content. Note that content may exist of pointers. In that case the pointers are copied, not what they refer to.

It may even be a good idea to pass your image-list around by reference. Saves a lot of smart-pointer copying, so it saves a lot of reference-count management, and may save a lot of locks/unlocks.

xtofl
+2  A: 

I think the boost::shared_ptr route is a good approach, as long as it doesn't matter that the Images don't get copied when the list is copied. You'll minimize copying but the reference counting will also clean things up for you when the last list is destroyed.

Nick Meyer
A: 

First, I would

typedef std::list<Image> ImageList;

If you just need to work with the one list, manipulating it, pass references to an ImageList itself. No need to copy anything that way. If you need to preserve the original list and make copies of it, boost::shared_ptr is the way to go.

Rob K
+1  A: 

Don't for get to look into the Boost pointer container. Copying boost::shared_ptr is cheap but not that cheap. Ref counting is not free either. If you're doing a lot of copying and have no need to share individual Image object, Boost pointer containers are the better way to go.

Shing Yip