Jump to content
  • Advertisement
Sign in to follow this  
random_thinker

what happens when a container of pointers is destroyed, are pointers destroyed?

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

Just a simple question: If I create something like: std::string my_function() { std::vector<some_pointer*> pVec; pVec.push_back(new some_pointer); pVec.push_back(new some_pointer); pVec.push_back(new some_pointer); <some action that uses pVec and produces a std::string result> return result; } When pVec goes out of scope, what happens to the pointers? Are they destroyed too?

Share this post


Link to post
Share on other sites
Advertisement
No. The container won't destroy them; you'll have to destroy them yourself or use a container of smart pointers.

Share this post


Link to post
Share on other sites
Just to clarify some terminology the pointers are destroyed, its the pointed at things which aren't destroyed. (sorry, it just seems people mix the two together all the time)

Share this post


Link to post
Share on other sites
... But please, first be sure that you can't (and know why not) just simplify:


std::string my_function()
{
std::vector<some_class> vec;
vec.push_back(some_class());
vec.push_back(some_class());
vec.push_back(some_class());

<some action that uses vec and produces a std::string result>

return result;
}

Share this post


Link to post
Share on other sites
Hi Zahlman,

I don't have a choice (I don't think). I typically avoid pointers like the plague, but in this case I need dynamic allocation because I will load data files that may vary in size between runs and need to size the vector dynamically at runtime.

Share this post


Link to post
Share on other sites
That doesn't mean you need to use pointers -- you're probably thinking of how you'd have to handle it if you didn't have vector. vector's resizable; this will work fine:

int count = readNumberOfThingsFromFile();

std::vector<Thing> things; // No pointers!
things.reserve(count);

for(int i = 0; i < count; ++i)
{
Thing thing = readThingFromFile();
things.push_back(thing);
}


If you don't know the size beforehand, it will still work, you will just have to omit the reserve() call which will cause a (entirely trivial here) perf issue since more reallocations of the underlying memory will occur.

Share this post


Link to post
Share on other sites
You guys are right, I don't need pointers within a vector in this case; thanks for clearing my head. Sometimes I just get too close to the trees and can't see the forest...

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!