Thread: C++ Linked List Memory Leak - Help Please!

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    1

    C++ Linked List Memory Leak - Help Please!

    So this is a CS lab that we're doing and my code passes all the tests as far as removing, inserting, clearing, and all that. However afterward it fails due to memory leaks. The Valgrind result points to where I'm initializing new nodes in my program but I'm setting them null at the end. Is that not what you must do to "clear up" the memory? I did Java in high school so all this memory management is new to me.

    Here's my LinkedList.cpp
    Code:
    #include "LinkedList.h"
    #include "Node.h"
    #include <stdlib.h>
    using namespace std;
    
    void LinkedList::insertHead(int value)
    {
       /*ONEBLOCKLOST*/
    //If value is negative,exit
          if (value < 0) {
        return;
      }
      (int x = 0;
       x < size();
       x++) {
        if (this->at(x) == value) {
          return;
        }
    
      }
    
      Node *tmp = new Node(value);
    //If there is no head, make this node the head
      if (head == NULL) {
        head = tmp;
      }
    //Otherwise,point this node to the head, making this node the new head
      else {
        tmp->next = head;
        head = tmp;
      }
    
      tmp = NULL;
    }
    
    void LinkedList::insertTail(int value)
    {
    //If a negative number is being given, exit
      if (value < 0) {
        return;
      }
    //Iterate through the list to see if the value already exists in the list
      for (int x = 0; x < size(); x++) {
        if (this->at(x) == value) {
          return;
        }
      }
    
    //Construct a new Node
      new_Node = new Node(value);
    //If the list is empty, make this node the head
      if (head == NULL) {
        head = new_Node;
      }
    //Otherwise
      else {
        spot = head;
    //Iterate through the list until you reach the end
        while (spot->next != NULL) {
          spot = spot->next;
        }
    
    //Append the new node to the end
        spot->next = new_Node;
      }
    //Clear and remove the new_Node variable
      new_Node = NULL;
    }
    
    void LinkedList::insertAfter(int value, int insertionNode)
    {
    //If value is negative,exit
      if (value < 0 || insertionNode < 0) {
        return;
      }
    //Iteratethroughthelisttoseeifthevaluealreadyexistsinthelist
      for (int x = 0; x < size(); x++) {
        if (this->at(x) == value) {
          return;                   //Exit if it does
        }
      }
    
      bool exists = false;
      for (int x = 0; x < size(); x++) {
        if (this->at(x) == insertionNode) {
          exists = true;
        }
      }
      if (exists == false)
        return;
    
    //Construct a new node
      new_Node = new Node(value);
      spot = head;
    
    //Iteratethroughthelistuntilyoureachanodewho'svalueisequaltoinsertionNode
      while (spot->value != insertionNode) {
        spot = spot->next;
      }
    
      if (spot->next != NULL) {
        new_Node->next = spot->next;
      }
      Node *tmp = spot->next;
      spot->next = new_Node;
      new_Node->next = tmp;
    
    //Clear and remove the new_Node variable
      new_Node = NULL;
      tmp = NULL;
    }
    
    void LinkedList::remove(int value)
    {
    //Make sure the array is not empty or that the value is not negative
      if (head == NULL || value < 0) {
        return;
      }
    //Makesurethenodeexistsinthelist
      bool exists = false;
      for (intx = 0; x < size(); x++) {
        if (this->at(x) == value) {
          exists = true;
        }
      }
    
      if (exists == false)
        return;
    
    //Iftheheadistheonebeingremoved,simplyre-allocatetheheadasthenextnode
      else if (head->value == value) {
        head = head->next;
      } else {
        spot = head;
    //Start at the beginning
        while (spot->next->value != value) {
          spot = spot->next;
        }
        Node *tmp = spot->next;
        spot->next = spot->next->next;
    //Just delete the node
        delete tmp;
      }
    }
    
    void LinkedList::clear()
    {
      while (head != NULL) {
        remove(head->value);
      }
    }
    
    int LinkedList::at(int index)
    {
      if (index < 0 || index > size()) {
        return -1;
      }
    //Otherwiseiteratethroughtheloopn#oftimes
      spot = head;
      for (int n = 0; n < index; n++) {
        spot = spot->next;
      }
    //Ifwereachanullpointintthelist,
      if (spot == NULL) {
        return -1;
      }
    //Otherwisereturntherespectivevalue
      else
        return spot->value;
    }
    
    int LinkedList::size()
    {
      int n = 0;
      spot = head;                  //Start at the beginning
      while (spot != NULL) {
    //Iterate through the list
        spot = spot->next;
        n++;
      }
    //n=n+1;//ClearanddeletethespotNodevariable
      return n;
    }
    
    LinkedList::~LinkedList()
    {
    //Calltheclearfunction
      clear();
    }
    Thanks in advance!
    Last edited by Salem; 10-07-2013 at 10:51 PM. Reason: removed font abuse, so code tags work properly

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    In Java, maybe.

    In C, if you allocated it you must free it once and once only.
    After freeing it, setting it to NULL it good to do.

    In C++, every new should have a delete to match.
    Note: delete[] and delete are not the same; delete[] is for arrays.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list possible memory leak?
    By KBriggs in forum C Programming
    Replies: 17
    Last Post: 08-13-2010, 09:05 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. linked list memory leak
    By bazzano in forum C Programming
    Replies: 11
    Last Post: 09-29-2005, 08:07 AM
  4. help on linked list and memory leak
    By thinhare in forum C Programming
    Replies: 2
    Last Post: 09-01-2005, 08:48 AM
  5. linked list memory
    By AmazingRando in forum C Programming
    Replies: 5
    Last Post: 09-23-2003, 05:20 PM

Tags for this Thread