title: Deleting the back element in a linked list
body: <p>This is not a duplicate of the question with a similar name, This involves OOP and new and delete calls.</p>
  
  <p>I'm trying to write a function that will iterate to the back of my linked list and then delete the allocated memory in the heap of the last node. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>void LinkedList::delete_back(){
      if(head != NULL){
              ListNode *end = head;
              while(end-&gt;next != NULL)
                      end = end-&gt;next;
              delete end;
      }
      size--;
  }
  </code></pre>
  
  <p>And here is my class definitions:</p>
  
  <pre><code>class ListNode{
  
      public:
              Item data;
              ListNode *next;
  };
  class LinkedList{
  
      private:
              ListNode *head;
              int size;
  
      public:
              LinkedList();
              ~LinkedList(); 
              bool empty();
              void insert_front(Item i);
              void insert_back(Item i);
              void delete_front();
              void delete_back();
              void print();
  };
  </code></pre>
  
  <p>Annddddd.....this is the problem, I get spammed with error messages like this from valgrind, some stating invalid read of size 4, other stating invalid read of size 8:</p>
  
  <pre><code>==4385== Invalid read of size 4
  ==4385==    at 0x400CAA: LinkedList::print() (in /home/jon/jball2_lab06/linkedlist)
  ==4385==    by 0x400EDD: main (in /home/jon/jball2_lab06/linkedlist)
  ==4385==  Address 0x5a04f30 is 0 bytes inside a block of size 16 free'd
  ==4385==    at 0x4C2A4BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==4385==    by 0x400C5E: LinkedList::delete_back() (in /home/jon/jball2_lab06/linkedlist)
  ==4385==    by 0x400E99: main (in /home/jon/jball2_lab06/linkedlist) 
  </code></pre>
  
  <p>I'll post the rest of the errors if that helps but I don't feel like hitting space 4 times on 50 lines unless I need to.  Does anyone know what this could be? What am I doing wrong?</p>
  
  <p>UPDATE-----------------------
  I've edited the code to be:</p>
  
  <pre><code>void LinkedList::delete_back(){
      if(head != NULL){
              ListNode *end = head;
              ListNode *prev_end;
              while(end-&gt;next != NULL){
                      prev_end = end;
                      end = end-&gt;next;
              }
              prev_end-&gt;next = NULL;
              if(end != NULL) delete end;
              size--;
      }
  }
  </code></pre>
  
  <p>I'm now getting more invalid read of size 8/4 errors and invalid free/delete errors</p>
  
  <pre><code>==5294== Invalid free() / delete / delete[] / realloc()
  ==5294==    at 0x4C2A4BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  </code></pre>
  
  <p>and this:</p>
  
  <pre><code>==5294== Use of uninitialised value of size 8
  ==5294==    at 0x400C3D: LinkedList::delete_back() (in /home/jon/jball2_lab06/linkedlist)
  ==5294==    by 0x400EEC: main (in /home/jon/jball2_lab06/linkedlist)
  </code></pre>
  
  <p>Here is the test code im using:</p>
  
  <pre><code>    for(Item i = 50; i &lt; 100; i++){
              ll.insert_back(i);
              cout &lt;&lt; "Inserted [" &lt;&lt; i &lt;&lt; "] in back.\n";
      }
      ll.print();
      for(int i = 0; i &lt; 50; i++)
              ll.delete_back();
      cout &lt;&lt; "Removed 50 elements from the back.\n";
      ll.print();
  </code></pre>
  
  <p>This occurs when the last element is removed from the list using delete_back()</p>
  
  <p>UPDATE-------------------------</p>
  
  <p>The problem was that if end->next is null than the while loop will never execute, prev_end will never be initalized.  Posted answer with fixes implemented.</p>