Advertisement Jump to content
Sign in to follow this  
silverphyre673

delete vector of pointers

This topic is 5038 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.

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!