【问题标题】:Remove last node c++ (recursion): double free or corruption (fasttop)删除最后一个节点 c++ (recursion): double free or corruption (fasttop)
【发布时间】:2021-05-03 16:01:24
【问题描述】:

我正在尝试删除单链表中的最后一个节点,但一直遇到错误: 双重免费或损坏(fasttop):0x00000000024e9c20

void removeLast (node *& head) {
    if(head == NULL)
        return;
    if(head->next == NULL) {
        delete head;
        head = NULL;
        return;
    }
    removeLast(head->next);
}

【问题讨论】:

  • 您是使用valgrind 运行您的代码还是在启用地址清理程序的情况下构建?在这里给我们的建议还不够,所以请提供minimal reproducible example
  • 您选择不向我们展示您的某些代码,问题出在该代码中。
  • 如何新建节点?
  • @DipStax class node { public: int data; node * next; }; void create (node * & head);

标签: c++ recursion linked-list


【解决方案1】:

不要在你的函数中使用 (node *&head) 尝试使用 (node **head)。调用此函数的地方将参数作为 &head 发送。 例如。

int main()
{
     ...
     removelast(&head);
     ...
}

       

【讨论】:

  • 不幸的是,我必须使用 (node *&head) 和 removelast(head) 我不能使用 (node **head) 和 removelast(&head)
猜你喜欢
  • 2018-09-30
  • 2014-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
相关资源
最近更新 更多