我要检查堆栈空虚,但仍会遇到此错误
即时通讯171:char 16:运行时错误:引用绑定到未对准的地址0xbebebebebebebebec0ba for Type'int',它需要4个字节对准(stl_deque.h) 0xbebebebebebebebec0ba:注意:指针指向此处 摘要:undefinedbehaviorsanitizer:undefined-behavior/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../../../../../../ include/c+++++++++++++ser :180:16
即使我在使用堆栈进行任何操作之前要检查堆栈空虚,这是leetcode上最大的矩形
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
stack<int> h;
stack<int> b;
int maxa;
int n = heights.size();
h.push(heights[0]);
b.push(0);
for (int i = 1; i < n; i++) {
if (!h.empty() && !b.empty() && h.top() < heights[i]) {
h.push(heights[i]);
b.push(i);
continue;
}
if (!h.empty() && !b.empty() &&h.top() == heights[i]) {
continue;
}
while (!h.empty() && !b.empty() && h.top() > heights[i]) {
maxa = max(maxa, h.top() * (i - b.top()));
h.pop();
b.pop();
}
h.push(heights[i]);
b.push(b.top());
}
while (!h.empty()) {
maxa = max(maxa, (h.top() * (n - b.top())));
h.pop();
b.pop();
}
return maxa;
}
};
Im getting 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
even though I'm checking for stack emptiness before I do anything with a stack, this is largest rectangle in histogram on leetcode btw
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
stack<int> h;
stack<int> b;
int maxa;
int n = heights.size();
h.push(heights[0]);
b.push(0);
for (int i = 1; i < n; i++) {
if (!h.empty() && !b.empty() && h.top() < heights[i]) {
h.push(heights[i]);
b.push(i);
continue;
}
if (!h.empty() && !b.empty() &&h.top() == heights[i]) {
continue;
}
while (!h.empty() && !b.empty() && h.top() > heights[i]) {
maxa = max(maxa, h.top() * (i - b.top()));
h.pop();
b.pop();
}
h.push(heights[i]);
b.push(b.top());
}
while (!h.empty()) {
maxa = max(maxa, (h.top() * (n - b.top())));
h.pop();
b.pop();
}
return maxa;
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论