剑指offer 09,30 关于栈遇到的问题
Line 175: 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
原因分析:
在栈空的时候访问了栈元素就会报错。判断条件错误可能会导致错误访问。
stack<int>stack;
s.top();
s.pop();
if判断条件顺序错误,导致可会在栈在空的情况,先判断去访问栈顶元素导致错误。
if(x <= stack.top() ||stack.empty());
if(stack.empty() || x <= stack.top());
晚上刷题的时候又遇到类似的问题了!!!对自己无语滴很!!
Line 14: Char 16: runtime error: member access within misaligned address 0x000000000007 for type ‘ListNode’, which requires 8 byte alignment (ListNodeUtils.cpp) 0x000000000007: note: pointer points here SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ListNodeUtils.cpp:24:16
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *pre = head,*cur = NULL, *tmp = NULL;
while(pre != NULL){
tmp = pre->next;
pre->next = cur;
cur = pre;
pre = tmp;
}
return cur;
}
|