【问题标题】:runtime error: reference binding to misaligned address 0xbebebebebebec0ba for type 'int', which requires 4 byte alignment运行时错误:引用绑定到未对齐地址 0xbebebebebebebec0ba 类型“int”,需要 4 字节对齐
【发布时间】:2021-09-25 21:35:14
【问题描述】:

第 171 行:字符 16:运行时错误:引用绑定到未对齐地址 0xbebebebebebec0ba 类型为“int”,需要 4 字节对齐 (stl_deque.h) 0xbebebebebebec0ba:注意:指针指向这里 摘要:UndefinedBehaviorSanitizer:未定义行为/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_deque.h :180:16

我在 leetcode 上解决了这个问题 Link to question

我的方法是使用 2 个堆栈来存储 2 个链表的值,然后在计算总和的同时将它们一个一个弹出,然后我计划将总和保存在一个名为 ans 的向量中,然后再次更改值通过将向量的值分配给链表。

这是我的代码:

ListNode* addll(ListNode* l1, ListNode* l2, vector<int> ans)
{
    ListNode* temp;
    temp=l1;
    int i=0;
    int sizee=ans.size();
    while(temp!=NULL)
    {
        temp->val=ans[i];
        i++;
        temp=temp->next;
    }
    if(i<sizee)
    {
        temp=l1;
        while(temp->next!=NULL)
        {
            temp=temp->next;
        }
        l2->val=ans[i];
        temp->next=l2;
        l2->next=NULL;
        
    }
    return l1;
}




ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    stack<int> s1;
    stack<int> s2;
    ListNode *temp=l1;
    vector<int> ans;
    while(temp!=NULL)
    {
        s1.push(temp->val);
        temp=temp->next;
    }
    temp=l2;
    while(temp!=NULL)
    {
        s2.push(temp->val);
        temp=temp->next;
    }
    int carry=0;
    while(!s2.empty() && !s1.empty())
    {
        if(s1.top()+s2.top()+carry<10)
        {
            ans.push_back(carry+s1.top()+s2.top());
            carry=0;
            s1.pop();
            s2.pop();
        }
        else
        {
            ans.push_back((carry+s1.top()+s2.top())%10);
            carry=1;
            s1.pop();
            s2.pop();
            
        }
    }
    int j;
    while(!s2.empty())
    {
        j=s2.top()+carry;
        if(j<10)
        {
            ans.push_back(j);
            carry=0;
        }
        else
        {
            j=j%10;
            ans.push_back(j);
            carry=1;
        }
      
        s2.pop();
    }
    while(!s1.empty())
    {
      
        j=s2.top()+carry;
        if(j<10)
        {
            ans.push_back(j);
            carry=0;
        }
        else
        {
            j=j%10;
            ans.push_back(j);
            carry=1;
        }
      
        s2.pop();
    }
    if(carry!=0)
    {
        ans.push_back(carry);
        carry=0;
    }
    reverse(ans.begin(),ans.end());
    int size1=0,size2=0;
    temp=l1;
    while(temp!=NULL)
    {
        size1++;
        temp=temp->next;
    }
    temp=l2;
    while(temp!=NULL)
    {
        size2++;
        temp=temp->next;
    }
    if(size1>size2)
    {
        return addll(l1,l2,ans);
    }
    else
    {
        return addll(l2,l1,ans);
    }
}
};

实现部分没有问题我自己检查过,我得到的唯一问题就在这里。

 while(!s2.empty())
    {
        j=s2.top()+carry;
        if(j<10)
        {
            ans.push_back(j);
            carry=0;
        }
        else
        {
            j=j%10;
            ans.push_back(j);
            carry=1;
        }
      
        s2.pop();
    }
    while(!s1.empty())
    {
      
        j=s2.top()+carry;
        if(j<10)
        {
            ans.push_back(j);
            carry=0;
        }
        else
        {
            j=j%10;
            ans.push_back(j);
            carry=1;
        }
      
        s2.pop();
    }

它给了我一个运行时错误,但是当我将其更改为

 while(!s2.empty())
    {
        ans.push_back(s2.top()+carry);
        carry=0;
        s2.pop();
    }
    while(!s1.empty())
    {
        ans.push_back(s1.top()+carry);
        carry=0;
        s1.pop();
    }

运行时错误消失了,但很明显,由于其错误的逻辑,它给了我一个错误的答案。有人可以帮我找出上述程序中的错误,因为这是我以前从未见过的错误。

【问题讨论】:

    标签: c++ vector data-structures linked-list stack


    【解决方案1】:

    您在这部分尝试访问s2 而不是s1

    while(!s1.empty())
        {
          
            j=s2.top()+carry; // here
            if(j<10)
            {
                ans.push_back(j);
                carry=0;
            }
            else
            {
                j=j%10;
                ans.push_back(j);
                carry=1;
            }
          
            s2.pop(); // and here
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2019-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多