c++ - 地址 sanitizer :DEADLYSIGNAL

标签 c++ data-structures stack queue

我在leetcode的以下代码中使用`st.pop()时收到此错误:

AddressSanitizer:DEADLYSIGNAL
=================================================================
==31==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x0000003a1925 bp 0x7ffd7b62dce0 sp 0x7ffd7b62dcd0 T0)
==31==The signal is caused by a READ memory access.
==31==Hint: this fault was caused by a dereference of a high value address (see register values below).  Dissassemble the provided pc to learn which register was used.
    #8 0x7f3d81a1e0b2  (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
AddressSanitizer can not provide additional info.
==31==ABORTING
代码如下:
class Solution {
public:
    string removeOuterParentheses(string S) {
        stack<char> st;
        int count=0;
        string ns;
        for(int i=0;i<S.size();i++)
        {
            if(S[i] == 40 && count++ > 0)
            {
                st.push(S[i]);
                ns+=S[i];
            }
            if(S[i] == 41 && count-- > 0)
            {
                st.pop();
                ns+=S[i];
            }
        }
        return ns;
    }
};

最佳答案

如果您有输入字符串"(some text)",则条件S[i] == 40 && count++ > 0将始终评估为false。当S[i]=='('为true时,由于比较后count++ > 0递增,因此count为false。但是,count递增,这样第二个条件S[i] == ')' && count-- > 0最终将为true。然后您点击st.pop()。由于st为空,将导致问题。
另外,如果您修改了代码,例如通过编写S[i] == 40 && ++count > 0不会删除括号,因为名称暗示了

关于c++ - 地址 sanitizer :DEADLYSIGNAL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64969581/

相关文章:

c++ - 为什么这个交换函数调用不明确?

c++ - 为什么需要类而不是结构

Java ConcurrentSkipListMap : adding atomically another Collection object

mysql - 你会如何设计这个数据库?

c - 堆栈无法成功将数字插入其中

c - 为什么打印链表内容时会出现段错误?

ruby - Ruby 的执行栈是什么样子的?

c++ - 我应该实现自己的 TCP/IP 套接字超时吗?

Java:由于字符而从数组中删除项目

c++ - += 在没有 boost 的 vector 上