C ++无法找到错误,双重自由或损坏(fasttop)

时间:2013-08-16 22:10:35

标签: c++ operator-overloading runtime-error

我从我的程序中得到以下输出:

$ ./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";
}

这就是重载&gt;&gt;运营商。

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

1 个答案:

答案 0 :(得分:2)

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

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