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

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -