title: How to correctly deallocate a c++ vector?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/8546239/how-to-correctly-deallocate-or-delete-a-c-vector">How to correctly deallocate or delete a c++ vector?</a>  </p>
  </blockquote>
  
  
  
  <p>I'm having some problems trying to delete memory that I've allocated in a vector. Even though I call list.clear(), it's not deallocating the memory.</p>
  
  <p>So I have some code like this in a template-based class called Set</p>
  
  <pre><code>template &lt;class T&gt;
  class Set {
  public:
      // stuff
  private:
      int size;
      std::vector&lt;T&gt; list;
  };
  </code></pre>
  
  <p>And in the constructor, I have allocated memory for the vector. So I call list = new std::vector;</p>
  
  <p>For your interest, here is my copy constructor and assignment operator that I've also witten where I also allocate memory for the vector:</p>
  
  <pre><code>template &lt;class T&gt;
  Set&lt;T&gt;::Set(const Set&amp; aSet)
  {
      size = aSet.size;
      list-&gt;clear();
      list = new vector&lt;T&gt;;
      for (int i = 0; i &lt; size; ++i) {
          list[i] = aSet.list[i];
      }
  }
  
  template &lt;class T&gt;
  Set&lt;T&gt;&amp; Set&lt;T&gt;::operator=(const Set&amp; right)
  {
      if (this != &amp;right) {
          list-&gt;clear();
          size = right.size;
          list = new vector&lt;T&gt;;
          for (int i = 0; i &lt; size; ++i) {
              list[i] = right.list[i];
          }
      }
      return (*this);
  }
  </code></pre>
  
  <p>In the destructor, I just have list.clear() to delete all elements and then deallocate the memory.</p>
  
  <p>But the problem is, when I run valgrind on my .out file, it's telling me that I've definitely lost some memory and I don't know why it's telling me this. I've read some questions here on Stackoverflow but I've basically tried everything. I tried clear() and then delete on the vector but that didn't work. I then tried to erase(list.begin(), list.end()) but that didn't work too. </p>
  
  <p>My thought process is that I'm using a Set *aSet = new Set; in my main class and since an int is not an object, it's not being freed when I call list.clear(). Is this right? How would I go about deleting the memory correctly?</p>
  
  <p>Thanks for any help. </p>
  
  <p>Edit1 = changed list* to setList</p>
  
  <p>My new constructors and the assignment operator:</p>
  
  <pre><code>template &lt;class T&gt;
  Set&lt;T&gt;::Set(const Set&amp; aSet)
  {
      size = aSet.size;
      setList.clear();
      setList = aSet.setList;
  }
  
  template &lt;class T&gt;
  Set&lt;T&gt;&amp; Set&lt;T&gt;::operator=(const Set&amp; right)
  {
      if (this != &amp;right) {
          setList.clear();
          size = right.size;
          setList = right.setList;
      }
      return (*this);
  }
  </code></pre>
  
  <p>Valgrind still reports I have the same amount of lost memory though. In my destructor I still have list.clear()</p>
  
  <p>Valgrind log:</p>
  
  <pre><code>==11398== 
  ==11398== HEAP SUMMARY:
  ==11398==     in use at exit: 62,969 bytes in 352 blocks
  ==11398==   total heap usage: 540 allocs, 188 frees, 68,046 bytes allocated
  ==11398== 
  ==11398== LEAK SUMMARY:
  ==11398==    definitely lost: 8,624 bytes in 14 blocks
  ==11398==    indirectly lost: 1,168 bytes in 5 blocks
  ==11398==      possibly lost: 4,829 bytes in 56 blocks
  ==11398==    still reachable: 48,348 bytes in 277 blocks
  ==11398==         suppressed: 0 bytes in 0 blocks
  ==11398== Rerun with --leak-check=full to see details of leaked memory
  </code></pre>