删除链表,内存泄漏C++
Delete linked list, memory leak C++
我编写了一个链表实现。我实现了一组方法,当然还有删除列表方法。然而,当我编译它并用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、引用、构造函数/析构函数、类方法。
相关文章:
- valgrind-hellgrind与泄漏检查的结果不同
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 尽管遵循了规则,内存泄漏在哪里
- 为什么调用堆栈数组会导致内存泄漏
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 使用模板类的自定义列表类型中的内存泄漏
- 为什么以下C++代码中存在内存泄漏?
- Klocwork Inside的资源泄漏
- OpenCV 我应该使用智能指针来防止内存泄漏吗?
- 我是否生成线程并导致内存泄漏?
- 多线程程序中出现意外的内存泄漏
- 为什么此函数会导致内存泄漏?
- 在 C++ 库中使用cythonized python时内存泄漏
- 需要帮助查找内存泄漏
- C++功能泄漏内存,我是C++新手,不确定如何解决
- 瓦尔格林德的内存泄漏使用新的
- CPP 中的瓦尔格林德和记忆泄漏:"Conditional jump or move depends on uninitialised values"
- 无法找出我的代码中的内存泄漏