Tag Archives: AddressSanitizer:DEADLYSIGNAL error

How to Solve LeetCode Error: AddressSanitizer:DEADLYSIGNAL

LeetCode Error: AddressSanitizer:DEADLYSIGNAL
AddressSanitizer:DEADLYSIGNAL

AddressSanitizer:DEADLYSIGNAL
42ERROR: AddressSanitizer: SEGV on unknown address (pc 0x00000034e832 bp 0x7ffdffb45790 sp 0x7ffdffb45540 T0)
42The signal is caused by a READ memory access.
42Hint: 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.
#3 0x7f75a82060b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
AddressSanitizer can not provide additional info.
42ABORTING

 

class Solution {
public:
    bool isValid(string s) {
          int n = s.size();
          if(n % 2 == 1){
              return false;
          }
          unordered_map<char, char> pairs = {
              {')','('},//key value
              {']','['},
              {'}','{'}
          };
          stack<char> stk;
          for(char ch:s){
              if(pairs.count(ch)){//Intrinsically check whether the pairs have elements with the given key ch
                  if(stk.top() != pairs[ch] || stk.empty()){
                      return false;
                  }
                  stk.pop();
              }else{
                  stk.push(ch);
              }
          }
          return stk.empty();
    }
};

Error reporting:

modification:

if(stk.empty() || stk.top() != pairs[ch])

Leetcode error: AddressSanitizer:DEADLYSIGNAL [How to Solve]

leetcode error:

AddressSanitizer:DEADLYSIGNAL ================================================================= ==43==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x00000034ca17 bp 0x7ffe82f08070 sp 0x7ffe82f07f40 T0) ==43==The signal is caused by a READ memory access. ==43==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. #3 0x7f952a7800b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) AddressSanitizer can not provide additional info. ==43==ABORTING

As you can see, the unknown address was accessed.
Reason:
The code accesses the top element of the stack for an empty stack

else if(s[i] == '}')
{
    if(st.top() == '{')
    {
       st.pop();
    }else
    {
       return false;
    }
}

Solution:

Just join the blank judgment

else if(s[i] == '}')
{
    if(!st.empty() && st.top() == '{')
    {
        st.pop();
    }else
    {
        return false;
    }
}