If i have a list of pointers, and I use the erase function, do I need to delete the object if it was allocated with new? Or is this taken care of for me?
Printable View
If i have a list of pointers, and I use the erase function, do I need to delete the object if it was allocated with new? Or is this taken care of for me?
No, all allocated memory with new must have a matching delete.
The STL won't delete the memory for you, only toss aside the pointer.
Or use a list of some smart pointer type instead of a plain ol' pointer, then it would be taken care of.
Yes, RAII classes are recommended. You can have a look at boost's shared_ptr.
It may be available as std::tr1::shared_ptr too.Quote:
You can have a look at boost's shared_ptr.
Or to avoid this overhead, you can use Boost's pointer_containers.
And of course make sure that you really do need/want to store pointers in your container. There are times when it's not necessary and storing objects is preferred.