简体   繁体   English

AddressSanitizer: SEGV on unknown address 0x000000000000 是什么意思?

[英]What does AddressSanitizer: SEGV on unknown address 0x000000000000 mean?

I'm solving a leetcode question and getting this error.我正在解决一个 leetcode 问题并收到此错误。 I have no idea what this mean as I'm relatively new to C++. It appears to disappear when I remove the else inside the else if.我不知道这是什么意思,因为我对 C++ 比较陌生。当我删除 else if 中的 else 时,它似乎消失了。

AddressSanitizer:DEADLYSIGNAL
=================================================================
==32==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000000383e8c bp 0x7ffc55bebe50 sp 0x7ffc55bebd20 T0)
==32==The signal is caused by a READ memory access.
==32==Hint: address points to the zero page.
    #3 0x7f2222e3982f  (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
AddressSanitizer can not provide additional info.
==32==ABORTING

My code:我的代码:

class Solution {
public:
    bool isValid(string s) {
        stack<char> stk;
        int flag=1;
        for(int i=0; i< s.length();i++){
            if(s[i]=='('||s[i]=='{'||s[i]=='['){
                stk.push(s[i]);
                flag=0;
            }
            else { //if (s[i]==')'||s[i]=='}'||s[i]==']'){
                if(s[i]==')'&&stk.top()=='('){
                    stk.pop();
                    flag=1;
                }
                 else if(s[i]==']'&&stk.top()=='['){
                    stk.pop();
                    flag=1;
                }
                else if(s[i]=='}'&&stk.top()=='{'){
                    stk.pop();
                    flag=1;
                }
                else
                    return false;
            }
        }
        if(flag==0)
            return false;
        else
            return true;
    }
};

It means that you are dereferencing a null pointer somewhere in your code.这意味着您正在代码中的某处取消引用 null 指针。 gdb would be a better tool to debug this problem. gdb 将是调试此问题的更好工具。 Run gdb program -ex r until it crashes.运行gdb program -ex r直到它崩溃。 Then print stacktrace with bt to see what was going wrong.然后用bt打印 stacktrace 看看出了什么问题。

That is a segmentation fault because of a deref of a null pointer.这是一个分段错误,因为 null 指针的 deref。

My guess is that you are querying the top element of an empty stack (deque).我的猜测是您正在查询空堆栈(双端队列)的顶部元素。 If the container has never been non-empty, it might hold a null pointer.如果容器从来不是非空的,它可能会保存一个 null 指针。

The documentation for std::deque<..>::back , which is what is called by std::stack<..>::top confirms that this exhibits UB:std::deque<..>::back的文档,即std::stack<..>::top所调用的文档证实了这展示了 UB:

Returns reference to the last element in the container.返回对容器中最后一个元素的引用。

Calling back on an empty container causes undefined behavior.回调空容器会导致未定义的行为。

I ran your code for the same problem on leetcode and this statement was faulty.我在 leetcode 上针对同样的问题运行了你的代码,这个语句是错误的。

  if(s[i]==') &&stk.top()=='(')

error was removed by with the help of a boundary check.在边界检查的帮助下消除了错误。

  if(s[i]==')'&&!stk.empty() &&stk.top()=='(')

Similarly for two other if statements.其他两个 if 语句也是如此。 Now the code is not giving any error's but logic is incorrect somewhere.现在代码没有给出任何错误,但逻辑在某处不正确。 It is giving wrong results for "([]" testcase.它为“([]”测试用例给出了错误的结果。

you need to handle the case where the input string start by any character meant to be poped, for example ')' or '(', because in this case you are poping from and empty stack already.您需要处理输入字符串以任何要弹出的字符开头的情况,例如')'或'(',因为在这种情况下,您已经从空堆栈中弹出。

if(s[i]==')'&&stk.top()=='('){

In the above line before stk.top() check whether the stack is empty or not.....means stk.size()>0 .stk.top()之前的上述行中,检查堆栈是否为空.....表示stk.size()>0

This will remove the error.这将消除错误。

The issue is with the following line and similar lines:问题在于以下行和类似行:

if(s[i]==')'&&stk.top()=='('){ if(s[i]==')'&&stk.top()=='('){

When executing the top method of stack it returns a reference to the top most element of the stack which is not present in your case (Ref: https://www.geeksforgeeks.org/stack-in-cpp-stl/ ).执行堆栈的顶部方法时,它返回对堆栈的最顶部元素的引用,该元素在您的情况下不存在(参考: https://www.geeksforgeeks.org/stack-in-cpp-stl/ )。

So my advice would be to either check this condition before of check it within the if block.所以我的建议是在 if 块中检查之前检查这个条件。

check stk.empty() in else loop first or a better sollution would be -首先在 else 循环中检查stk.empty()或者更好的解决方案是 -

 if (s.length() < 2 || s[0] == ']' || s[0] == ')' || s[0] == '}')
        {
            return false;
        }

        stack<char> stk;

        for (int i = 0; i < s.length(); i++)
        {
            if (s[i] == '(' || s[i] == '[' || s[i] == '{')
            {
                stk.push(s[i]);
            }
            else
            {
                if (stk.empty()) return false;
                if (s[i] == ')' && stk.top() != '(')
                    return false;
                if (s[i] == ']' && stk.top() != '[')
                    return false;
                if (s[i] == '}' && stk.top() != '{')
                    return false;

                stk.pop();
            }
        }
        return stk.empty();

AddressSanitizer will give you a clue for find MemoryLeak (in especial in mult thread reference memory) AddressSanitizer 将为您提供查找 MemoryLeak 的线索(特别是在多线程引用内存中)

and I think Edward Cullen is right solution我认为 Edward Cullen 是正确的解决方案

include.stk.empty() before stk.top() == '(' include.stk.empty() 在 stk.top() == '(' 之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “==7645==ERROR: AddressSanitizer: SEGV on unknown address 0x1003fd37e049 (pc 0x55c8da2c3605 bp 0x7fffe9c30110 sp 0x7fffe9c300f0 T0)”是什么意思 - What does "==7645==ERROR: AddressSanitizer: SEGV on unknown address 0x1003fd37e049 (pc 0x55c8da2c3605 bp 0x7fffe9c30110 sp 0x7fffe9c300f0 T0)" mean 为什么我得到以下代码 AddressSanitizer: SEGV on unknown address error - why am i getting for the following code AddressSanitizer: SEGV on unknown address error SEGV_ACCERR 是什么意思? - What does SEGV_ACCERR mean? 运行时错误:基数为0x000000000000的指针索引表达式溢出到0xffffffffffffffffff以进行频率排序 - Runtime error: pointer index expression with base 0x000000000000 overflowed to 0xffffffffffffffff for frequency sort ZTV,ZTS,ZTI在gdb x / nfu“ vtable_address”的结果中意味着什么? - What does ZTV,ZTS,ZTI mean in the result of gdb x/nfu “vtable_address”? “x += x &amp; (-x)”是什么意思? - What does “x += x & (-x)” mean? “#define XX”是什么意思? - What does it mean by “#define X X”? 查找“未知地址上的 SEGV”的原因,由 READ 访问引起 - Find the cause of 'SEGV on unknown address', cause by READ access “ char x []”是什么意思? - What does 'char x[]' mean? X f()是什么意思? - What does X f() mean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM