0
votes

Hi I ran my program through valgrind and this is the report

Heap Summary: in use at exit: 8 bytes in 1 blocks total heap usag: 1 allocs, 0 frees, 8 bytes allocated Leak Summary: definitely lost: 8 bytes in 1 blocks

This is my program

int main() {    
NodeData nodedata1(1, 'a');
List list1;
list1.insert(&nodedata1);

   return 0;
}
//---my List class
class List {
public:
   List(); 
   bool insert(NodeData*);                     // insert one Node into list
   bool isEmpty() const; 
private:
   struct Node {              // the node in a linked list
      NodeData* data;         // pointer to actual data, operations in NodeData
      Node* next;
   };
   Node* head;                                 // pointer to first node in list
};
// my bool insert method
bool List::insert(NodeData* rightPtr) {  
   Node* newPtr = new Node;
   newPtr->data = rightPtr;    
   if (isEmpty() || *newPtr->data < *head->data) {
      newPtr->next = head;                           
      head = newPtr;
   }
   else {
      Node* current = head; 
      while (current->next !=NULL && *current->next->data < *newPtr->data) {
           current = current->next;
      }
      newPtr->next = current->next;
      current->next = newPtr;
   }
   return true;
}
2
List doesn't have a destructor that delete these nodesNeel Basu
Unless this is an exercise or homework, you should really save yourself all this trouble and use std::list instead.Thomas
You should create a destructor for your List to destroy all your Nodes when the List is destroyed.cgmb

2 Answers

4
votes

Object created by declaring it will be destroyed before its scope ends. Object that created using new will not be destroyed unless you manually delete it. The following example demonstrates lifetime of object.

#include <iostream>
using namespace std;

class Data {
    protected:
        char* name;
    public:
        Data(char* n) {
            name = n;
            cout << name <<  " is created." << endl;
        }

        ~Data() {
            cout << name <<  " is destroyed." << endl;
        }
};

void ScopeDemo() {
    Data obj1("Obj1");
    Data* obj2 = new Data("Obj2");
}

int main()
{
    ScopeDemo();
    return 0;
}

In modern operating system, memory used by application will be released when the application terminated.


In your example, you will need to manually delete when List object is destroyed (which is via object destructor) and when you remove node from your List.

List::~List() {
    if (!isEmpty()) {
        Node* toDelete = head;
        while(toDelete != NULL) {
            Node* next = toDelete->next;
            delete toDelete;
            toDelete = next;
        }
    }
}
3
votes

You are dynamically allocating a Node in the insert method and never deleting it:

Node* newPtr = new Node;

You need to keep track of these Nodes and delete them in the List destructor, or have some other arrangement to handle the memory (i.e. pass the ownership of the Nodes to something that is in charge of deleting them at the right moment).