pointers,deallocation and vector in c++ -
i want know should explicitly deallocate pointers erase vector myvector.erase(); .
for example;
class sample1{ public: removesample2(sample2 * a) { if(// if find in samplelist1 vector index ){ // should call here delete or pointer ? samplelist1.erase(samplelist1.begin()+i); } } private: vector<int *> samplelist1; } class sample2{ public: // not important areas private: sample1 * example; }
there's no way possibly know, since don't know you're trying do. basic answer is:
if collection logically owns things in it, doing wrong. never use ordinary collections of ordinary pointers purpose. things go horribly wrong if you, example, copy collection. (either use ordinary collections of smart pointers or collections designed hold pointers , manage lifetimes of objects in them.)
if collection not own things in it, not
delete
pointers collection. cause other code uses pointers access object after has been deleted.
Comments
Post a Comment