title: C++ destructor of a class containing a vector of pointers
body: <p>I have a class that looks like this :</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  class MyClass
  {
      vector&lt;int*&gt; V;
  
  public:
      MyClass();        
      MyClass(int n);
      ~MyClass();
  };
  
  MyClass::MyClass()
  {             
      return;
  }
  
  MyClass::MyClass(int n)
  {
      int* T = new int[n];
      for(int i=0; i&lt;n; i++)
      {
          T[i]=i;
          V.push_back(&amp;(T[i]));
      }
      return;
  }
  
  MyClass::~MyClass()
  {
      for(int i =0; i&lt;V.size(); i++)
          delete V[i];
      return;
  }
  
  int main()
  {   
      MyClass C(5);
      return 0;
  }
  </code></pre>
  
  <ol>
  <li>What's wrong about my destructor? I get a "<strong>* glibc detected *</strong> ./a.out: free(): invalid pointer:..." error upon execution of this.</li>
  <li>Do you think I should use ptr_vector? I don't know if I have the courage to learn about those.</li>
  </ol>
  
  <p>Thanks in advance!</p>