gpt4 book ai didi

c++ - 存在内存泄漏问题的链表

转载 作者:行者123 更新时间:2023-11-30 02:15:43 24 4
gpt4 key购买 nike

此程序充当模板,用于生成具有唯一名称和颜色的兔子对象的链接列表。

int Nodes::insert()
{

Node* new_node = (Node*) malloc(sizeof(Node));

new_node->name = getName();

new_node->prev = NULL;
new_node->next = head;

if (head != NULL)
{
head->prev = new_node;
}

head = new_node;

return 0;
}

当节点(bunny)插入链表时,唯一属性是从创建节点(bunny)时返回的函数调用分配的。

new_node->name = getName();

函数调用工作正常,直到我调用它来创建多个节点导致此错误:

> *** Error in `./zBunny.exe': free(): invalid pointer: 0x00007f0e528dab78 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f0e5258d7e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f0e5259637a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f0e5259a53c]
./zBunny.exe[0x4016e8]
./zBunny.exe[0x401ef2]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f0e52536830]
./zBunny.exe[0x401569]
======= Memory map: ========
00400000-00404000 r-xp 00000000 08:10 33423400 /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
00604000-00605000 r--p 00004000 08:10 33423400 /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
00605000-00606000 rw-p 00005000 08:10 33423400 /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
01f52000-01f84000 rw-p 00000000 00:00 0 [heap]
7f0e4c000000-7f0e4c021000 rw-p 00000000 00:00 0
7f0e4c021000-7f0e50000000 ---p 00000000 00:00 0
7f0e5220d000-7f0e52315000 r-xp 00000000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52315000-7f0e52514000 ---p 00108000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52514000-7f0e52515000 r--p 00107000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52515000-7f0e52516000 rw-p 00108000 fc:01 5509816 /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52516000-7f0e526d6000 r-xp 00000000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e526d6000-7f0e528d6000 ---p 001c0000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528d6000-7f0e528da000 r--p 001c0000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528da000-7f0e528dc000 rw-p 001c4000 fc:01 5509806 /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528dc000-7f0e528e0000 rw-p 00000000 00:00 0
7f0e528e0000-7f0e528f6000 r-xp 00000000 fc:01 5509743 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e528f6000-7f0e52af5000 ---p 00016000 fc:01 5509743 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e52af5000-7f0e52af6000 rw-p 00015000 fc:01 5509743 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e52af6000-7f0e52c68000 r-xp 00000000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52c68000-7f0e52e68000 ---p 00172000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e68000-7f0e52e72000 r--p 00172000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e72000-7f0e52e74000 rw-p 0017c000 fc:01 3670249 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e74000-7f0e52e78000 rw-p 00000000 00:00 0
7f0e52e78000-7f0e52e9e000 r-xp 00000000 fc:01 5509685 /lib/x86_64-linux-gnu/ld-2.23.so
7f0e53073000-7f0e53079000 rw-p 00000000 00:00 0
7f0e5309c000-7f0e5309d000 rw-p 00000000 00:00 0
7f0e5309d000-7f0e5309e000 r--p 00025000 fc:01 5509685 /lib/x86_64-linux-gnu/ld-2.23.so
7f0e5309e000-7f0e5309f000 rw-p 00026000 fc:01 5509685 /lib/x86_64-linux-gnu/ld-2.23.so
7f0e5309f000-7f0e530a0000 rw-p 00000000 00:00 0
7ffe1d800000-7ffe1d821000 rw-p 00000000 00:00 0 [stack]
7ffe1d9b0000-7ffe1d9b2000 r--p 00000000 00:00 0 [vvar]
7ffe1d9b2000-7ffe1d9b4000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted

完整代码:

#include<iostream>
#include<fstream>
#include<vector>
#include<string>

using std::vector;
using std::string;
using std::cout;
using std::endl;


class Node
{
public:
int data;
string name;

Node* next;
Node* prev;
};

class Nodes{
private:
Node* head = NULL;

public:
int insert();
void display();
int randomGen(int n);
string getName();
void extract(std::vector<string> *vect, string fileName);

};

int Nodes::insert()
{
Node* new_node = (Node*) malloc(sizeof(Node));

new_node->name = getName();

new_node->prev = NULL;
new_node->next = head;

if (head != NULL)
{
head->prev = new_node;
}

head = new_node;

return 0;
}


int main()
{
Nodes control;
control.insert();
control.insert();
// control.insert();
// control.insert();
}

最佳答案

您正在使用“malloc”创建一个 Node 和其中的 std::string,但这不起作用。您需要改用 new。超出此范围的任何行为都是未定义的行为。 new 调用构造函数,这是设置对象正常工作所必需的。

除此之外,您并没有在析构函数中使用 delete 清理您的 Node 分配,因此在程序结束时它们被“泄露”——尽管操作系统无论如何都会回收内存。

关于c++ - 存在内存泄漏问题的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55821967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com