title: How to delete objects from vector of pointers to object?
body: <p>I am trying to understand how to delete a vector of pointers, and the pointed objects, in memory. I have started with a simple example, found in another thread, but I get "pointer being freed was not allocated" error. </p>
  
  <p>What I am doing wrong?</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;iostream&gt;
  
  int main(){
      std::vector &lt;int *&gt; vec;
  
      int a = 2;
      int * b = &amp;a;
  
      int c = 3;
      int * d  = &amp;c;
  
      vec.push_back(b);
      vec.push_back(d);
  
      for (int i = 0; i &lt; vec.size(); i++) {
          delete vec[i];
      }
      vec.clear();
  
  }
  </code></pre>