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:

  1. 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.)

  2. if collection not own things in it, not delete pointers collection. cause other code uses pointers access object after has been deleted.


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -