Search code examples
c++reference-countingstdlist

C++ Storing large data in std::list<> ..should I use reference counting?


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?


Solution

  • 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.