runtime error: reference binding to misaligned address 0xbebebebebebec0ba for type 'int', which requires 4 byte alignment

runtime error: reference binding to misaligned address 0xbebebebebebec0ba for type 'int', which requires 4 byte alignment

Line 171: Char 16: runtime error: reference binding to misaligned address 0xbebebebebebec0ba for type 'int', which requires 4 byte alignment (stl_deque.h) 0xbebebebebebec0ba: note: pointer points here SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /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();
    }

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

您正在尝试在这部分访问 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
    }