2. 两数相加 c++

2. 两数相加 - 力扣(LeetCode)

Line 82: Char 28: ================================================================= ==22==ERROR: AddressSanitizer: stack-use-after-return on address 0x7f7399c003a8 at pc 0x5651d0ef1e46 bp 0x7ffce5baa990 sp 0x7ffce5baa988 READ of size 8 at 0x7f7399c003a8 thread T0 #0 0x5651d0ef1e45 in ListNodeUtils::hasCycle(ListNode*) (solution+0x1aae45) #1 0x5651d0ee4d15 in void Serializer::serialize<ListNode>(ListNode* const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&) (solution+0x19dd15)

这个 AddressSanitizer (ASan) 错误

复制代码
ERROR: AddressSanitizer: stack-use-after-return
...
READ of size 8 at 0x7f7399c003a8
...
This frame has 2 object(s):
    [32, 48) 'node' (line 29) <== Memory access at offset 40 is inside this variable

说明你的程序 返回了一个指向栈上局部变量的指针 ,并在函数返回后使用了它 ------ 这是典型的 "栈对象使用后返回"(stack-use-after-return) 错误。

根本原因

你在某个函数(如 addTwoNumbers)中写了类似这样的代码:

cpp 复制代码
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode node(0);      // ← 栈上局部对象
    ListNode* head = &node; // ← 取栈对象地址
    // ... 操作 head ...
    return head;           // ❌ 返回栈对象地址!
}

ListNode* func() {
    ListNode dummy;        // 栈上节点
    ListNode* cur = &dummy;
    // 构建链表,cur 指向 dummy
    return dummy.next;     // 如果 dummy.next 指向其他栈对象,也可能出错
}

⚠️ 一旦函数返回,nodedummy 就被销毁,其内存不再有效。

但你返回了它的地址(或其成员的地址),后续使用(如 hasCycle 访问 next)就会读取已释放的栈内存 → 未定义行为(UB)

AddressSanitizer 检测到这一点,并报 stack-use-after-return

正确做法:在堆上分配节点

链表节点必须在 堆(heap) 上创建,才能在函数返回后继续使用:

cpp 复制代码
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode* head = new ListNode(0);
    ListNode* cur = head;
    // ... 
    return head->next; // head 是堆对象,head->next 也是堆对象
}

题解

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* phead = new ListNode();
        ListNode* p = phead;
        int inval = 0;
        while(l1 && l2){
            int x = l1->val+l2->val+ inval;
            inval = x/10;
            x = x%10;
            p->next = new ListNode(x);
            p=p->next;
            l1 = l1->next;
            l2=l2->next;
        }
        while(l1){
            int x = l1->val+ inval;
            inval = x/10;
            x = x%10;
            ListNode node(x);
            p->next = new ListNode(x);
            p=p->next;
            l1 = l1->next;
           
        }
        while(l2){
            int x = l2->val+ inval;
            inval = x/10;
            x = x%10;
            ListNode node(x);
            p->next = new ListNode(x);
            p=p->next;
            l2=l2->next;
        }
        if(inval){
            p->next = new ListNode(inval);
        }
        return phead->next;
    }
};
相关推荐
小小小米粒1 天前
Collection单列集合、Map(Key - Value)双列集合,多继承实现。
java·开发语言·windows
智者知已应修善业1 天前
【51单片机中的打飞机设计】2023-8-25
c++·经验分享·笔记·算法·51单片机
czhc11400756631 天前
C# 428 线程、异步
开发语言·c#
:1211 天前
java基础
java·开发语言
SilentSamsara1 天前
Python 环境搭建完整指南:从下载安装到运行第一个程序
开发语言·python
小短腿的代码世界1 天前
Qt文件系统与IO深度解析:从QFile到异步文件操作
开发语言·qt
智者知已应修善业1 天前
【51单片机按键调节占空比3位数码管显示】2023-8-24
c++·经验分享·笔记·算法·51单片机
harder3211 天前
RMP模式的创新突破
开发语言·学习·ios·swift·策略模式
jinanwuhuaguo1 天前
OpenClaw工程解剖——RAG、向量织构与“记忆宫殿”的索引拓扑学(第十三篇)
android·开发语言·人工智能·kotlin·拓扑学·openclaw
Rust研习社1 天前
使用 Axum 构建高性能异步 Web 服务
开发语言·前端·网络·后端·http·rust