gpt4 book ai didi

C++找不到错误、双重释放或损坏(fasttop)

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:49:19 24 4
gpt4 key购买 nike

我的程序得到以下输出:

$ ./list
Enter list 1: [1,2,3,4]
[
Enter list 2: [2,5,8,0]
[
[1,2,3,4]
[1,2,3,4]
*** Error in `./list': double free or corruption (fasttop): 0x0000000000f85100 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x80a46)[0x7fa0368d6a46]
./list[0x400d5f]
./list[0x400c62]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0x7fa036877ea5]
./list[0x400ae9]
======= Memory map: ========
00400000-00402000 r-xp 00000000 ca:01 410613 /home/ubuntu/list

...

ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
the endAborted (core dumped)

这是我的主要内容:

int main(){
pennlist::List l1,l2;

cout<<"Enter list 1:\t";
cin>>l1;
cout<<"Enter list 2:\t";
cin>>l2;
cout<<l1<<endl<<l2<<endl;
cout<<"the end";
}

这是重载的 >> 运算符。

istream& operator >>(istream& ins, List& write_me){
char discard;
write_me.head = new node;
write_me.current = write_me.head;
node* temp = write_me.head;
ins>>discard;//get [
cout<<discard<<endl;

while(discard != ']'){
ins>>temp->data;
write_me.count += 1;
temp->to_tail = new node;
temp->to_head = temp;
temp = temp->to_tail;
ins>>discard; //get , or ]
}
write_me.tail = temp;
temp = NULL;
return ins;
}

我也重载了 =、~ 和复制 ctr,并在添加这些函数之前和之后得到相同的错误。

我不知道如何解决这个错误,请帮忙。

编辑

Here is the code for the destructor:
~List{
delete head;
delete current;
delete tail;
}

最佳答案

我更改了析构函数,程序现在可以正常运行了。谢谢大家。

~List{
if (head != current && tail != current)
delete current;
if (tail != head)
delete tail;
delete head;
}

关于C++找不到错误、双重释放或损坏(fasttop),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18282864/

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