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;
}
You are dynamically allocating a
Nodein theinsertmethod and never deleting it:You need to keep track of these
Nodesand delete them in theListdestructor, or have some other arrangement to handle the memory (i.e. pass the ownership of theNodesto something that is in charge of deleting them at the right moment).