Jump to content
  • Advertisement
Sign in to follow this  
Grahf750

stl list erase w/ new

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

I have created an stl list to store student* I add a student like this: students.push_back(new Student(nextStuId, name, grade, zip)); so I need to delete the students when I am done. If I delete a student in the list using erase do I need to call delete or will it take care of it? Also to delete everything when I am done should I do something like this?
	for(studentsIter = students.begin(); studentsIter != students.end(); studentsIter++)
	{
		delete *studentsIter;
	}

Share this post


Link to post
Share on other sites
Advertisement
if u are using a list the corect form is:

for(studentsIter = students.begin(); studentsIter != students.end(); studentsIter++)
{
delete studentsIter;//<- you delete the object not the pointer
}

Share this post


Link to post
Share on other sites
If you have an STL list of plain pointers, then you have to delete the memory manually, the STL will not do it for you. When you call erase, it just removes the pointer from the list, not the data it points to.

Use a smart pointer like boost::shared_ptr. It will automatically delete what it points to when it is destroyed.

www.boost.org

This way, you don't have to worry about accidently forgetting to delete something, etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by Grahf750
If I delete it will it be there for me to erase or will it be gone?


The pointer is still there (even if it points to deleted memory) waiting for you to erase it. What you are doing is simply removing that element from the list. It could have anything (or nothing) in that element and the stl list won't care. So delete the pointer then erase the element from the list and you're golden.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gink
yeah or auto_ptr works

No it doesn't. auto_ptr doesn't have the correct copy semantics for use in STL containers. A standards-compatible compiler will refuse to allow it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gink
yeah or auto_ptr works


No it doesn't, you can't store auto_ptr in an STL container, because it transfers ownership when it is copied. Use boost::shared_ptr instead, it's reference counted.

Edit: Beaten

Share this post


Link to post
Share on other sites
Lol

Quote:
It will automatically delete what it points to when it is destroyed.


I meant it automatically deletes the object it refers to when it's destroyed as well

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!