I tried this:
....
vector<players*> player;
for (int i = 0; i<10; i++)
{
player.push_back(new players());
}
...
And I wonder if I need to free memory for the vector? If so, how?
I tried this:
....
vector<players*> player;
for (int i = 0; i<10; i++)
{
player.push_back(new players());
}
...
And I wonder if I need to free memory for the vector? If so, how?
If you need to store pointers to things in a container, you should either store some sort of smart pointer (like
std::tr1::shared_ptr
orboost::shared_ptr
) or use a container designed for storing pointers, like those found in the Boost Pointer Container Library.If you do store bare pointers, you need to remember to delete the pointers in the container before the container is destroyed. This includes any time that an exception is thrown that might cause the container to be destroyed. Getting this right is tedious, bug-prone, and wholly unnecessary given the facilities mentioned in the previous paragraph.
since you are creating new players(), you will have to delete them. probably best to iterate through the vector deleting the players and then clean up your vector.
Yes, you do need to
delete
them yourself. The vector is only going to "destruct" the pointers (which does nothing).If you can, use the Boost pointer containers library, and you won't have to worry about it. However, if you can't you need to wrap the container up. Consider an exception is thrown between the time the container is populated and the time its elements are deleted. You will not execute the element delete code and leak.
A simple wrapper might look like:
Use it like:
This is a simple wrapper. Any
pop_back()
,erase()
, etc. operations will have the wrong effect. I strongly suggest using Boost.One may think of using a container of
auto_ptr
. Contrarily, this is a bad idea; the copy-semantics of anauto_ptr
prevent it from working. The best option is to get rid of the dynamic allocation, if possible.