Reputation: 75
I want to know that should i explicitly deallocate the pointers which are erase from a vector by myvector.erase(); .
For example;
Class Sample1{
public:
removeSample2(Sample2 * a)
{
if(// if i find that a is in my sampleList1 vector with index i ){
// should i call here delete or something like that for a pointer ?
sampleList1.erase(sampleList1.begin()+i);
}
}
private:
vector<Int *> sampleList1;
}
Class Sample2{
public:
// not important areas
private:
Sample1 * example;
}
Upvotes: 1
Views: 88
Reputation: 27460
You shall delete the element by yourself. vector did not allocate that element and so it will not free it.
Upvotes: 0
Reputation: 182753
There's no way we could possibly know, since we don't know what you're trying to do. But the basic answer is:
If the collection logically owns the things in it, you are doing it all wrong. Never use ordinary collections of ordinary pointers for this purpose. Things will go horribly wrong if you, for example, copy the collection. (Either use ordinary collections of smart pointers or collections specifically designed to hold pointers and manage the lifetimes of the objects in them.)
If the collection does not own the things in it, then do not delete
pointers from the collection. That will cause other code that uses those pointers to access an object after it has been deleted.
Upvotes: 2