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])

Read More: