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

Started by
14 comments, last by Zahlman 16 years, 8 months ago
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?
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.
Advertisement
No. The container won't destroy them; you'll have to destroy them yourself or use a container of smart pointers.
Thanks again Si,

I was worried about this; I think that I'll try to use smart pointers as you suggest, it seems a safe and reliable approach.
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.
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)
... 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;}
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.
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.
Which seems to be exactly what Zahlman's code does.
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.
if you still want to use pointers in a vector, have a look at boost::ptr_vector, it will call delete on pointers when being destructed.
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...
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.

This topic is closed to new replies.

Advertisement