Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


STL vector of pointers to objects

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

OK, since my last post on this same topic I've become a little bit more knowledgable about STL's vector class. Here's the problem though,
I have a vector whose elements are pointers to objects. Documentation says that an erase will not deallocate memory allocated to that object (for the reason that the vector controls its own memory not the memory of the pointer of the object).
How do I deallocate the memory that my object is using from vector?

Share this post

Link to post
Share on other sites
The STL vector allocates the memory which it allocated, but not what you allocated with new... and then inserted as pointer into the vector

To delete the contents of a vector you have to go throught it and delete the memory the pointer points to and then let the vector do the rest.

=> if you want to erase !and! delete member n
delete stl_vector_object[n]; // delete memory
stl_vector_object.erase( n /* position n */ );

In a destructor you should write sth. like :

for ( int i = 0; i < stl_vector_object.size(); ++i )
delete stl_vector_object[i];

-> Aidan

Share this post

Link to post
Share on other sites

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