0
votes
Here is my code.

#include <bits/stdc++.h> 
using namespace std; 

class Node { 
public: 
    int data; 
    Node* next; 
}; 

void printList(Node* n) 
{ 
    while (n != NULL) { 
        cout << n->data << " "; 
        n = n->next; 
    } 
} 

void delete_list(Node *n)
{
    Node *tmp = NULL;
    tmp = new Node();
    while (n != NULL){
        tmp=n->next;
        delete (Node *)n;
        n=tmp;
    }
    delete (Node *)tmp;
}


int main() 
{ 
    Node* head = NULL; 
    Node* second = NULL; 
    Node* third = NULL; 

    head = new Node(); 
    second = new Node(); 
    third = new Node(); 

    head->data = 1; 
    head->next = second;

    second->data = 2;
    second->next = third; 

    third->data = 3;
    third->next = NULL; 

    printList(head); 

    // Delete the entire list
    delete_list(head);

    return 0; 
} 
  • HEAP SUMMARY:enter code here in use at exit: 16 bytes in 1 blocks total heap usage: 6 allocs, 5 frees, 73,792 bytes allocated 16 bytes in 1 blocks are definitely lost in loss record 1 of 1 at 0x4C3017F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x10895E: delete_list(Node*) (demo.cpp:24) by 0x108A87: main (demo.cpp:58) LEAK SUMMARY: definitely lost: 16 bytes in 1 blocks indirectly lost: 0 bytes in 0 blocks possibly lost: 0 bytes in 0 blocks still reachable: 0 bytes in 0 blocks suppressed: 0 bytes in 0 blocks For counts of detected and suppressed errors, rerun with: -v ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
2
Why do you create a node when deleting the list? And before doing anything else with the node, you assign another one, losing last pointer to the node just created – which is your memory leak (or at least one of).Aconcagua
void delete_list(Node *n) { Node *tmp = NULL; // tmp = new Node(); while (n != NULL){ tmp=n->next; delete (Node *)n; n=tmp; } // delete (Node *)tmp; }Rajeev

2 Answers

2
votes
void delete_list(Node *n)
{
    Node *tmp = NULL;

    // this is your memory leak:
    // you just created a new node ...
    tmp = new Node();

    while (n != NULL)
    {
        // ... and very first thing you do is assigning another node to the only
        // pointer holding the node created just before
        tmp = n->next;

        delete (Node *)n;
        n = tmp;
    }

    // apart from cast being obsolete (tmp HAS appropriate type!)
    // tmp is already nullptr and you don't delete anything at all
    delete (Node *)tmp;
}

This example is shows quite well why it's a good idea to keep variables as local as possible:

while (n != NULL)
{
    Node* tmp = n->next;
    delete (Node *)n;
    n = tmp;
}

You simply cannot get tempted to assign anything without need (or even create irrelevant objects) or trying to delete something that doesn't exist any more...

0
votes

Yes i did the same thing. thanks you guys.

void delete_list(Node *n)
{
        Node *tmp = NULL;
//      tmp = new Node();
        while (n != NULL){
        tmp=n->next;
        delete (Node *)n;
        n=tmp;
    }
//    delete (Node *)tmp;
}

Now there is no memory leakage. :) Memcheck, a memory error detector Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info Command: ./a.out 1 2 3
FILE DESCRIPTORS: 3 open at exit. Open file descriptor 2: /dev/pts/3 Open file descriptor 1: /dev/pts/3 Open file descriptor 0: /dev/pts/3 HEAP SUMMARY: in use at exit: 0 bytes in 0 blocks total heap usage: 5 allocs, 5 frees, 73,776 bytes allocated All heap blocks were freed -- no leaks are possible For counts of detected and suppressed errors, rerun with: -v ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)