链表c++的递归删除
recursive deletion of linked list c++
你能告诉我为什么下一个代码的实现会导致错误吗:
void del (num*&head) {
num*temp = 0;
if (head!=0) {
temp = head;
delete temp;
del (head->next);
}
else return;
}
错误:
Access violation reading location 0xcdcdcdc1.
而下一个代码工作正常:
void del (num*&head) {
if (head!=0) {
del (head->next);
delete head;
}
else return;
}
删除temp
将使head
无效,因为它们都指向同一个对象。head->next
试图访问已删除的对象,给出未定义的行为。您应该存储head->next
,这样就不必访问已删除的对象:
if (head) {
num * temp = head->next;
delete head;
del (temp);
}
您正在运行调试构建,因此删除的内存被设置为垃圾,而不是保留其旧内容。这意味着,尝试将从中读取的值用作指针将很快触发访问冲突,从而帮助您找到问题的根源。
第二个版本会等到您处理完对象后再删除它,所以没有未定义的行为。
然而,递归是个坏主意,因为如果列表太长,它可能会导致堆栈溢出。迭代会更安全:
while (head) {
num * temp = head->next;
delete head;
head = temp;
}
您正在访问head->next
,而实际上您已经删除了前一行的head
。
另一种方法是:
void del (num*&head) {
if (head!=0) {
num* temp = head->next;
delete head;
del (temp);
}
else return;
}
在第一个版本中,
temp = head;
delete temp;
del (head->next);
temp
和head
指向同一节点,则该节点被删除并变为无效。但它是在的最后一行访问的
del (head->next);
第二个版本没有这个问题。
temp = head;
delete temp; // head is dead too!
del (head->next); // dereferencing disposed pointer
只要您使用指向同一内存的指针进行操作,删除任何指针都会使内存被删除。因此,即使你不删除其他指针,它们也指向一块垃圾(甚至指向另一个人拥有的内存)
这样想:-
当你完成
temp = head;// it means temp and head are talking about the same person,i.e, the same memory location.
delete temp; //the person(memory location) to which the both of them are pointing is dead;
del (head->next); //Now you trying to kill the "ghost" of the person means you are trying to delete already deleted node.
但在第二种情况下,递归调用刚好高于
delete head;
因此递归调用首先进行到末尾,然后从末尾开始删除节点,如下所示:-
如果您有n个节点,递归调用的摘要可以查看为:-
del (list); // call from main.
del (list->head); //recursive call.
del (list->head->head);//recursive call.
del (list->head->head->head);//recursive call.
等等…………
所以当最后一个节点出现时,其CCD_ 9是CCD_。所以这里没有像你的第一个案例那样造成伤害。
相关文章:
- 从类型列表中递归删除重复项会导致编译器堆空间错误 (VS2017)
- 在对象指针上调用 Delete 是否会递归删除其动态分配的成员
- C++删除/(递归)对象销毁问题
- 在c++中使用递归从字符串中删除所有辅音
- 如何递归删除Qt3DWindow根实体中的所有节点?
- 递归树遍历/分支删除的隔离错误
- 以递归方式从链表中删除节点
- 如何使用 Boost Hana 删除元编程递归
- 如果 x,递归不起作用,我的函数只能删除第一个元素
- 编写递归算法以从链表中删除元素.编写递归算法以将元素添加到链表中
- 递归删除预处理器宏
- 从链接列表(通过递归)中删除元素
- 递归模板参数删除
- 不太清楚为什么我的递归链表删除函数有效?我很想解释一下
- 是否可以从此函数中删除递归
- 如何使用递归删除二叉搜索树中的节点
- 如何在没有递归的情况下删除链接列表
- C++无法在递归中删除对象的常量
- 如何在visual C++2013上递归删除二叉树
- 共享指针递归地删除递归数据结构,堆栈溢出