Jump to content
  • Advertisement
Sign in to follow this  
Broni

Pointers held in STL List

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

Quick question about storing pointers within a STL List. Say I have a class class A { A(); ~A(); blah } and I create an STL list std::list<A*> listOfA; and in some function I add A's to this list void addToList() { listOfA.push_back(new A()); } Do I have to delete the memory held within the list when the program finishes?How is memory freed from STL list? Thanks, Edwin

Share this post


Link to post
Share on other sites
Advertisement
Yeah thats what I thought.

That seems to put me in alot of bother, I think I'll look into smart pointers.

Share this post


Link to post
Share on other sites
If you use smart pointers, make sure to use a reference counted pointer of some kind, since auto_ptr isn't safe for use in containers.

This is probably a dumb quesiton, but do you actually need to use pointers here? (i.e., are you using polymorphism?)

Share this post


Link to post
Share on other sites
No I'm not using polymorphism. I thought it was necessary to use std::list<T*> in this situtation also. As once I leave that function will the list keep a copy of my object T, even thought the original object is now out of scope?

Share this post


Link to post
Share on other sites
The list will keep a copy of the object, yes. Not the original. Make sure you overload operator=() and the copy constructor too, unless your class is a POD (Plain Old Data) type (I.e. no pointers).

What does your class contain?

Share this post


Link to post
Share on other sites
Quote:
Original post by Broni
No I'm not using polymorphism. I thought it was necessary to use std::list<T*> in this situtation also. As once I leave that function will the list keep a copy of my object T, even thought the original object is now out of scope?


Standard containers keep a copy of their value type (whether that value is T or T*). As long as the list is in scope, it's contents are good.

Share this post


Link to post
Share on other sites
Ok. So if I dont want to keep copys I can use T* instead of T, but I'll have to manually delete these objects- is that correct?

Share this post


Link to post
Share on other sites
The list only cares about the type it's holding. If it's holding instances (not pointers) of A objects, it will make copies of them and make sure they get destroyed and all that stuff for you, no need to worry about anything.

If it's holding pointers to A objects, it will copy the pointers, and destroy the pointers (and by destroy I don't mean delete what they're pointing to), but the actual objects that the pointers are pointing to will be completely ignored by the list. It doesn't care about what the pointers are pointing to, all it cares about are the pointers themselves.

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!