mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
完善0707.设计链表C++版本,在delete指针之后赋值为null,在注释中 说明了野指针问题
This commit is contained in:
@ -133,6 +133,11 @@ public:
|
|||||||
LinkedNode* tmp = cur->next;
|
LinkedNode* tmp = cur->next;
|
||||||
cur->next = cur->next->next;
|
cur->next = cur->next->next;
|
||||||
delete tmp;
|
delete tmp;
|
||||||
|
//delete命令指示释放了tmp指针原本所指的那部分内存,
|
||||||
|
//被delete后的指针tmp的值(地址)并非就是NULL,而是随机值。也就是被delete后,
|
||||||
|
//如果不再加上一句tmp=nullptr,tmp会成为乱指的野指针
|
||||||
|
//如果之后的程序不小心使用了tmp,会指向难以预想的内存空间
|
||||||
|
tmp=nullptr;
|
||||||
_size--;
|
_size--;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1447,3 +1452,4 @@ impl MyLinkedList {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user