leetcode 203 ERROR: AddressSanitizer: heap-use-after-free on address

出现问题题目:、​​、​​​​203. 移除链表元素 - 力扣(LeetCode)

题解:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台 

原因:删除内存后不应该用指针再次使用这块内存

错误代码1:

p=head;
 pre=head;
 while(head&&(head->val==val))
        {
            ListNode *tmp=head;
            head=head->next;
            delete tmp;
           // return head;
            //delete p;
        }
while(p)
{
......
}
       

p先指向head,后面本来的head已经由delete tmp删除,但`while(p)`**又使用了指向原来head的p**,出现错误

错误代码2:

 ListNode *tmp2=p;
 pre->next=p->next;
 delete tmp2;
 p=p->next;

已删除p指向的内存,但仍使用p

正确代码见题解 

 

相关推荐

  1. Leetcode 3035. Maximum Palindromes After Operations

    2024-01-06 00:54:01       57 阅读
  2. LeetCode1005. Maximize Sum Of Array After K Negations

    2024-01-06 00:54:01       39 阅读
  3. Leetcode 3002. Maximum Size of a Set After Removals

    2024-01-06 00:54:01       62 阅读
  4. LeetCode2696. Minimum String Length After Removing Substrings

    2024-01-06 00:54:01       49 阅读
  5. LeetCode2789. Largest Element in an Array after Merge Operations

    2024-01-06 00:54:01       44 阅读
  6. LeetCode --- 2119. A Number After a Double Reversal 解题报告

    2024-01-06 00:54:01       24 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-06 00:54:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-06 00:54:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-06 00:54:01       87 阅读
  4. Python语言-面向对象

    2024-01-06 00:54:01       96 阅读

热门阅读

  1. 前端加密库 jsencrypt的使用

    2024-01-06 00:54:01       61 阅读
  2. 字母异位词分组【哈希】

    2024-01-06 00:54:01       67 阅读
  3. SSH 端口转发:如何将服务绑定到本地 IP 地址

    2024-01-06 00:54:01       60 阅读
  4. leetcode链表相关题目

    2024-01-06 00:54:01       61 阅读
  5. 笙默考试管理系统-MyExamTest----codemirror(62)

    2024-01-06 00:54:01       50 阅读
  6. neo4j配置详解

    2024-01-06 00:54:01       65 阅读
  7. 保障企业数据安全的29个最佳实践

    2024-01-06 00:54:01       58 阅读
  8. Pointnet++环境配置(Windows11和ubuntu)及训练教程

    2024-01-06 00:54:01       66 阅读
  9. ==和equals

    2024-01-06 00:54:01       62 阅读
  10. LeeetCode 206

    2024-01-06 00:54:01       52 阅读
  11. 防抖节流的应用场景

    2024-01-06 00:54:01       52 阅读