删除链表,内存泄漏C++

Delete linked list, memory leak C++

本文关键字:泄漏 C++ 内存 链表 删除      更新时间:2023-10-16

我编写了一个链表实现。我实现了一组方法,当然还有删除列表方法。然而,当我编译它并用Valgrind-3.10.1检查内存泄漏时,它会显示内存泄漏:

    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)

这是代码:

#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);
}

从valgrind的输出来看,问题出在insert before/after方法上,但我不知道如何解决。

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

node(9)在哪里被删除?

应该使用的东西是:std::list、引用、构造函数/析构函数、类方法。