RunTime error when attempting the Valid Parentheses problem in c++

  algorithm, boolean, boolean-logic, c++, parentheses

In the code below, I attempt to find whether the parentheses ‘(‘, ‘[‘, ‘{‘, ‘)’, ‘]’, ‘}’ are valid. For more details on the concept of valid parentheses see the following link on leetcode: https://leetcode.com/problems/valid-parentheses/

I get the following error message, in spite of my code working in test cases:

AddressSanitizer:DEADLYSIGNAL

==31==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x00000034475e bp 0x7ffefd12c010 sp 0x7ffefd12bea0 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.
#3 0x7f41d395a0b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) AddressSanitizer can not provide additional info.
==31==ABORTING

Here is my code:

 #include <stack>
    
    class Solution {
    public:
        bool isValid(string s) {
            stack<char> paren;
            for(auto & c: s)
            {
                if(c == '(')
                    paren.push(c);
                else if (c == '[')
                    paren.push(c);
                else if(c == '{')
                    paren.push(c);
                else if(c == ']' || c == ')' || c == '}')
                {
                       if( c == ')' && paren.top() == '(')
                            paren.pop();
                       else if( c == ']' && paren.top() == '[')
                                paren.pop();
                       else if( c == '}' && paren.top() == '{')
                                paren.pop();
                        else
                                return false;
                }
            }
            return paren.empty();
        }
    };

Any idea, why it is not working, because I do not understand the error message.

Source: Windows Questions C++

LEAVE A COMMENT