title: How do I deallocate contents of a vector upon its destruction?
body: <p>As far as I know that for both vector declarations as:</p>
  
  <pre><code>//TYPE 1
  std::vector&lt;cls&gt; vec;     //cls is user defined datatype(A class)
  </code></pre>
  
  <p>Memory for vector is allocated on stack and the memory of contents in the vector is allocated on heap.</p>
  
  <p>It is true for the below declaration as well(Correct me if I am wrong):</p>
  
  <pre><code>//TYPE 2
  std::vector&lt;cls*&gt; vec;    //cls is user defined datatype(A class)
  </code></pre>
  
  <p>Now when the vector in Type 1 goes out of scope, memory is deallocated for the objects stored in it.</p>
  
  <p>But what happens in type 2 if I insert elements as below(assuming that I have the proper overloaded constructor) and then the vector goes out of scope:</p>
  
  <pre><code>vec.push_back(new cls(5));
  </code></pre>
  
  <p>I explicitly tried calling clear but the destructor was not invoked. Will the memory be automatically be deallocated and the destructors be called. If not then how to achieve that.</p>
  
  <p>Also, where is memory allocated for the vector as well as the contents if I declare vector as:</p>
  
  <pre><code>std::vector&lt;cls*&gt; *vec = new std::vector&lt;cls*&gt;;
  </code></pre>