我要检查堆栈空虚,但仍会遇到此错误

发布于 2025-02-01 21:07:39 字数 1404 浏览 1 评论 0原文

即时通讯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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文