title: Still Reachable allocated memory using valgrind
body: <p>I'm getting ready for an object oriented class starting in a few weeks, but I'm having trouble with the concept of deallocating memory. RIght now I'm just allocating memory to the first node of the stack, and then trying to deallocate it. If anyone could help me find what I am doing wrong with my destructor, it would be greatly appreciated. Thanks in advance!</p>
  
  <p>The output I am getting from valgrind memcheck is:</p>
  
  <pre><code>==13653== 8 bytes in 1 blocks are still reachable in loss record 1 of 1
  ==13653==    at 0x4847DA4: operator new(unsigned int) (vg_replace_malloc.c:328)
  ==13653==    by 0x10A2B: Stack::AddNode(int) (Stack.C:25)
  ==13653==    by 0x10923: main (Main.C:5)
  ==13653==
  ==13653== LEAK SUMMARY:
  ==13653==    definitely lost: 0 bytes in 0 blocks
  ==13653==    indirectly lost: 0 bytes in 0 blocks
  ==13653==      possibly lost: 0 bytes in 0 blocks
  ==13653==    still reachable: 8 bytes in 1 blocks
  ==13653==         suppressed: 0 bytes in 0 blocks
  </code></pre>
  
  <p><strong><em>Code:</em></strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  class Stack {
    private:
  
      struct node{
        int data;
        node * prev;
      };
  
      node * stackptr;
  
    public:
  
     Stack() {
       stackptr = nullptr;
     }
  
     ~Stack() {
        cout &lt;&lt; "Calling destructor" &lt;&lt; endl;
        node * p1 = stackptr;
        node * delptr = nullptr;
  
        while(p1 != nullptr) {
          delptr = p1;
          p1 = p1-&gt;prev;
          delptr-&gt;prev = nullptr;
          cout &lt;&lt; "Deleteing " &lt;&lt; delptr-&gt;data &lt;&lt; " from the stack" &lt;&lt; endl;
          delete delptr;
        }
     }
  
     void AddNode(int data) {
        node * n = new node;
        n-&gt;data = data;
  
        if(stackptr == nullptr) {
          stackptr = n;
          stackptr-&gt;prev = nullptr;
        }
        else {
           n-&gt;prev = stackptr;
           stackptr = n;
        }
     }
  };
  
  int main() {
    Stack s;
    s.AddNode(1);
    s.AddNode(2);
    return 0;
  }
  </code></pre>
  
  <p>EDIT: I have finished my AddNode class. I am still having a similar issue, but with two memory leaks now (The two calls to AddNode in main).</p>
  
  <p>EDIT: Using C++ version 6.3.0 and valgrind version 3.13.0</p>