Jump to content
  • Advertisement
Sign in to follow this  
MARS_999

STL and copies of objects...

This topic is 2543 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

I am looking at some code and it just dawned on me that the STL containers e.g. std::queue makes a copy of each object you place in it. So I am thinking what a waste of RAM. So how is everyone getting around this? Are they using pointers vs. a whole object? to only send in I am guessing a 4kb address vs. a whole object structure? Or are they coding their own container classes, or is there something I am missing in the STL that would allow me to place my objects in the container and not double up my objects.

Thanks....

Share this post


Link to post
Share on other sites
Advertisement

I am looking at some code and it just dawned on me that the STL containers e.g. std::queue makes a copy of each object you place in it. So I am thinking what a waste of RAM. So how is everyone getting around this? Are they using pointers vs. a whole object? to only send in I am guessing a 4kb address vs. a whole object structure? Or are they coding their own container classes, or is there something I am missing in the STL that would allow me to place my objects in the container and not double up my objects.

Thanks....


the STL containers can do nothing 'but' store copies, any container you implemented yourself must also do this or else it would simply be wrong.

eg:
{
Vec object;
container.add(object);
} //object is destroyed, but reference to it exists in container.

Share this post


Link to post
Share on other sites
Well, you could use move semantics if your compiler supports the current C++ standard, otherwise it's either value semantics (copying your object) or address semantics (for example, using smart refcounted ponters, either the standard ones in C++11 or some third-party library like boost, or possibly risking it all and using raw pointers).

For the most part, you will find the vast quantities of core memory available with today's systems makes the worry of storing a few bytes per object a very low-priority worry. If you're targeting a small phone or embedded system, yeah, you may need to roll your own container. Just remember, the data needs to live somewhere, and a well-defined, well-tested container is a pretty good place to put it.

Share this post


Link to post
Share on other sites
Ok thanks all for the help. I was just checking to see if I was correct in my observations, and my thoughts are what you all suggested, either use BOOST or move to the new C++11 standard, or last but not least make my own containers... yuck....

Thanks!

Share this post


Link to post
Share on other sites
If your objects don't allocate resources (i.e. call the new operator in their constructor) then there's no point worrying about it. Creating the temporary object is trivial and the alternatives are unlikely to be more efficient.

If your objects are complex, the best solution if it's available to you is to use move constructors. As a rule, all classes where you explicitly define a copy constructor should also have move constructors. This will give your code a performance boost with zero restructuring and hardly any effort on your part.

But if you can't use a c++11 compiler (there are actually varying degrees of support but iirc all the major compilers have move constructors), then you can use smart pointers. But on the topic of wasting memory, smart pointers to small dynamically allocated objects will waste (compared to storing by value) significant amounts so if your environment is truly memory limited, you might be better off sticking with the superfluous copy constructor/destructor call.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!