title: C++ Memory management for vector of Objects
body: <p>I have a few questions/examples about how memory management works for Vectors. </p>
  
  <pre><code>    vector&lt;int&gt; vec1(10);
      vector&lt;int&gt; vec2(10);        
      vec1 = vec2;
  </code></pre>
  
  <p>In a case like this, vec1's old memory is unreachable now. Is it still a memory leak here or would vec2's old memory realize there nothing referencing it and get cleaned up?</p>
  
  <p>In another example</p>
  
  <pre><code>struct foo
  {
     vector&lt;int&gt; foo_vec(50);
     int* arr; 
  }
  
  
  void myFunction()
  {
     vector&lt;foo&gt; vec(10);
     vec[0].arr = new int[50];
     vec.erase(vec.begin());
  }
  </code></pre>
  
  <p>Since I used <code>erase()</code> on the first vector element which contained arr with the allocated memory, does <code>erase()</code> release that memory or do I need to release it manually prior to the erase? Also when <code>vec</code> goes out of scope, are all the <code>foo_vec</code> vectors in <code>vec</code> automatically cleaned up? Thanks for any help.</p>