Title: Runtime Error: Segmentation Fault (SIGSEGV)
Tags: algorithm;segmentation-fault;stack;postfix-notation;infix-notation
Question: I am trying to write a program to convert infix expression to postfix expression. But I am getting the error
```
Runtime Error: Segmentation Fault (SIGSEGV)
```
I know that
```
SIGSEGV is an error caused by an invalid memory reference
```
Algorithm used

Scan the infix expression from left to right.
If the scanned character is an operand, output it.
Else,
1 If the precedence of the scanned operator is greater than the precedence of the operator in the stack(or the stack is empty           or the stack contains a ‘(‘ ), push it.
2 Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. After doing that Push the scanned operator to the stack. (If you encounter parenthesis while popping then stop there and push the scanned operator in the stack.)
If the scanned character is an ‘(‘, push it to the stack.
If the scanned character is an ‘)’, pop the stack and and output it until a ‘(‘ is encountered, and discard both the parenthesis.
Repeat steps 2-6 until infix expression is scanned.
Print the output
Pop and output from the stack until it is not empty.

Code
```class Solution
{
    int prec(char a)
    {
        if (a == '^')
            return 3;
        else if (a == '*' || a == '/')
            return 2;
        else if (a == '+' || a == '-')
            return 1;
        else
            return -1;
    }
public:
    string infixToPostfix(string str)
    {
        stack<char&gt; stk;
        string ans;
        for (auto e : str)
        {
            if ((e &gt;= 'a' &amp;&amp; e <= 'z') || (e &gt;= 'A' &amp;&amp; e <= 'Z'))
                ans += e;
            else if (e == '(')
                stk.push(e);
            else if (stk.empty())
                stk.push(e);
            else if (prec(stk.top()) < prec(e))
            {
                if (e != ')')
                    stk.push(e);
                else
                {
                    while (!stk.empty() &amp;&amp; stk.top() != '(')
                    {
                        ans = ans + stk.top();
                        stk.pop();
                    }
                    if (stk.empty())
                        continue;
                    else
                        stk.pop();
                }
            }
            else if (prec(stk.top()) &gt;= prec(e))
            {
                while (prec(stk.top()) &gt;= prec(e) &amp;&amp; !stk.empty())
                {
                    if (prec(stk.top()) &gt; prec(e))
                    {
                        ans = ans + stk.top();
                        stk.pop();
                    }
                    else
                    {
                        //same precedence so checking for associativity
                        if (stk.top() == '^') //associativity from R-&gt;L
                            stk.push(e);
                        else                //associativity from L-&gt;R
                        {
                            ans = ans + stk.top();
                            stk.pop();
                        }
                    }
                }
                stk.push(e);
            }
        }
        if (stk.empty())
            return ans;
        else
        {
            while (!stk.empty())
            {
                ans = ans + stk.top();
                stk.pop();
            }
            return ans;
        }
    }
};
```
I have tried a lot to find out the fault in the code, but I was unable to figure it out. Please help me.

Comment: @ScottHunter It just prints "Runtime Error: Segmentation Fault (SIGSEGV)".

Comment: Thanks @abhishek_naik your suggesion worked.

Comment: Can you try using `!stk.empty()` everywhere before you do `stk.top()`?  I see it not being done in a few places.  Also, the order is important.  So instead of `prec(stk.top()) >= prec(e) && !stk.empty()`, do `!stk.empty() && prec(stk.top()) >= prec(e)`

Comment: When you use a debugger, what is the *first* thing the program does incorrectly?

Comment: What debugger are you using?



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:04/08/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<algorithm><segmentation-fault><stack><postfix-notation><infix-notation>', 'Title': 'Runtime Error: Segmentation Fault (SIGSEGV)', 'ViewCount': 43, 'id': 67006927, 'user_AboutMe': '', 'user_AccountId': 10628474.0, 'user_DisplayName': 'Biplab Prasad', 'user_Reputation': 35.0, 'user_Views': 11.0, 'user_id': 7827432.0}}