gpt4 book ai didi

c++ - 如何修复此错误 : set a breakpoint in malloc_error_break to debug

转载 作者:行者123 更新时间:2023-11-28 04:19:52 26 4
gpt4 key购买 nike

我正在学习编写链表代码。我目前正在尝试删除链表的第一个元素。所以我写了下面的代码。它对第一个元素工作正常。但它显示第二个元素的错误。

void deleteFirst(Node* head){

Node* temp = head;
head->data = temp->next->data;
head->next = temp->next->next;
free(temp);}

给定链表:

2 3 4 5 6 7

第一次删除后它显示给我:

 3 4 5 6 7 

第二次删除后显示:

a.out(78094,0x7fff92d38380) malloc: 
*** error for object 0x7ffb39400370: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

最佳答案

你的 temp 和 head 都指向同一个内存。当你释放温度时,你也释放了头部。检查你的逻辑,祝你好运。

关于c++ - 如何修复此错误 : set a breakpoint in malloc_error_break to debug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55711442/

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