-2

The following code is in a function and result is a reference to a bool value.

trueValues and false Values are sets of chars that either result in a true or false statement.

stack<bool> operandStack;
for (int k = 0; k < postfix.size(); k++)
{
    char ch = postfix[k];
    if (ch != '!' && ch != '&' && ch != '|')
    {
        if (trueValues.contains(ch))
        {
            operandStack.push(true);
        }
        if (falseValues.contains(ch))
        {
            operandStack.push(false);
        }
    }
    else if (ch == '!' || ch == '&' || ch == '|')
    {
        bool operand2 = operandStack.top();
        operandStack.pop();
        bool operand1 = operandStack.top();
        operandStack.pop();
        switch(ch)
        {
            case '!':
                operandStack.push(operand1 != operand2);
            case '&':
                operandStack.push(operand1 && operand2);
            case '|':
                operandStack.push(operand1 || operand2);
        }
    }
}
result = operandStack.top();

Commenting out the result = line gets rid of the error message.

Ash Bal
  • 9
  • 1
  • It will crash if the stack is empty, and there are cases where this can happen. – Ian4264 Feb 03 '19 at 21:57
  • can you show trueValues & falseValues initialization? – Spinkoo Feb 03 '19 at 21:58
  • for the test case I'm working on right now I've made sure the stack isn't empty. – Ash Bal Feb 03 '19 at 22:00
  • trueValues and falseValues are doubly linked lists. I check to see if a letter in a string is in either of those sets and push either true or false onto operandStack. – Ash Bal Feb 03 '19 at 22:01
  • can you check the operandStack size at every iteration? – Spinkoo Feb 03 '19 at 22:09
  • Yes. The stack is never empty through the for loop. – Ash Bal Feb 03 '19 at 22:16
  • 1
    @AshBal Are the missing `break;` statements intended and you really want to fall through and execure all the statements after being transported to a specific `case` value label? – πάντα ῥεῖ Feb 03 '19 at 22:50
  • *It will crash if the stack is empty* It MAY crash. Undefined Behaviour is weird and awesome stuff. I mean awesome int the old, something to be feared avoided at all costs sense. – user4581301 Feb 03 '19 at 23:57

2 Answers2

0

First, I see that your "if (ch == '!' || ch == '&' || ch == '|')" has no purpose. There is nothing else they could be.

Also, if "postfix" is a std::string, I'm pretty sure size includes a terminating null character. So it's probably trying to pop something there.

Third, if this whole thing is inside of a function, and you are returning a reference as you suggest (I don't see the actual function prototype) you are trying to return a reference to a local stack variable that's about to be destroyed.

And, as Spinkoo pointed out, you are also missing breaks in your switch...

Bob Shaffer
  • 635
  • 3
  • 13
0

I figured out what was going wrong. Nothing with the code provided here. I had a mistake formatting the strings I was putting into the function. Sorry y'all, and thanks for the help.

Ash Bal
  • 9
  • 1