Brian Brown Brian Brown - 4 months ago 28
C++ Question

Delete linked list, memory leak C++

I wrote a linked list implementation. I implemented a set of methods, and of course, delete list method. However, when I compile it and check memory leaks with

Valgrind-3.10.1
it shows me memory leaks:

valgrind --leak-check=full ./listatmp
==3200== Memcheck, a memory error detector
==3200== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==3200== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==3200== Command: ./listatmp
==3200==
3 5 7 1024 9 1024 5 34 67
==3200==
==3200== HEAP SUMMARY:
==3200== in use at exit: 32 bytes in 2 blocks
==3200== total heap usage: 12 allocs, 10 frees, 192 bytes allocated
==3200==
==3200== 16 bytes in 1 blocks are definitely lost in loss record 1 of 2
==3200== at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3200== by 0x400871: node(int) (listatmp.cpp:16)
==3200== by 0x400BFA: main (listatmp.cpp:127)
==3200==
==3200== 16 bytes in 1 blocks are definitely lost in loss record 2 of 2
==3200== at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3200== by 0x400871: node(int) (listatmp.cpp:16)
==3200== by 0x400C2A: main (listatmp.cpp:128)
==3200==
==3200== LEAK SUMMARY:
==3200== definitely lost: 32 bytes in 2 blocks
==3200== indirectly lost: 0 bytes in 0 blocks
==3200== possibly lost: 0 bytes in 0 blocks
==3200== still reachable: 0 bytes in 0 blocks
==3200== suppressed: 0 bytes in 0 blocks
==3200==
==3200== For counts of detected and suppressed errors, rerun with: -v
==3200== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)


And here's the code:

#include <iostream>

struct Node
{
int value;
struct Node *next;
};

struct List
{
struct Node *first, *last;
};

Node* node(int v)
{
Node *newNode = new Node();
newNode->value = v;
newNode->next = NULL;
return newNode;
}

List* list()
{
List *newList = new List();
newList->first = NULL;
newList->last = NULL;
return newList;
}

List* insert(List* s, Node* n)
{
if (s->last == NULL)
{
n->next = s->last;
s->last = n;
s->first = n;
return s;
}

s->last->next = n;
s->last = s->last->next;
return s;
}

void print(List* s)
{
Node *tmp = s->first;
while(tmp)
{
std::cout << tmp->value << ' ';
tmp = tmp->next;
}
std::cout << '\n';
}

List* insertAfter(List* s, Node* curr, Node* n)
{
Node *currListNode = s->first;

while(currListNode != NULL)
{
if (currListNode->value == curr->value)
{
n->next = currListNode->next;
currListNode->next = n;
break;
}
currListNode = currListNode->next;
}

return s;
}

List* insertBefore(List* s, Node* curr, Node* n)
{
Node* prev = s->first;
Node* currListNode = s->first->next;

while(currListNode != NULL)
{
if(currListNode->value == curr->value)
{
break;
}
else
{
prev = currListNode;
currListNode = currListNode->next;
}
}

if(currListNode != NULL)
{
prev->next = n;
n->next = currListNode;
}

return s;
}

void deleteList(List *s)
{
Node * curr = s->first;
while (s->first != NULL)
{
s->first = s->first->next;
delete curr;
curr = s->first;
}
delete s;
}


int main(int argc, char *argv[])
{
List *myList2;
myList2 = list();

myList2 = insert(myList2, node(3));
myList2 = insert(myList2, node(5));
myList2 = insert(myList2, node(7));
myList2 = insert(myList2, node(9));
myList2 = insert(myList2, node(5));
myList2 = insert(myList2, node(34));
myList2 = insert(myList2, node(67));

myList2 = insertBefore(myList2, node(9), node(1024));
myList2 = insertAfter(myList2, node(9), node(1024));
print(myList2);

deleteList(myList2);
}


It seems from valgrind output that the problem is with insert before / after methods but I don't know how to fix it.

Answer
  myList2 = insertBefore(myList2, node(9), node(1024));

Where does node(9) get deleted?

Things you should be using instead: std::list, references, constructors/destructors, class methods.