Jump to content
  • Advertisement
Sign in to follow this  
silverphyre673

delete vector of pointers

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

Advertisement

struct delete_object
{
template <typename T>
void operator()(T *p){ delete p;}
};



use it like so:


std::for_each( myVector.begin(), myVector.end(), delete_object());

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
std::vector stuff;
for (unsigned int n = 0; n < stuff.size(); n++) { delete stuff[n]; }

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
std::vector stuff;
for (unsigned int n = 0; n < stuff.size(); n++) { delete stuff[n]; }


Prefer standard algorithms to explicit loops.

Share this post


Link to post
Share on other sites
Quote:
Original post by silverphyre673
This has probably been asked... but if I have a vector of pointers to some object, how should I delete the pointers so I don't get memory leaks?


Before you implement this, be sure that your vector "owns" the things it's pointing to (such that there won't be attempts anywhere else in the code to delete the same objects via other pointers to them). Smart pointers may make your life easier here.

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!