• Advertisement
Sign in to follow this  

delete vector of pointers

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