Need some help with debugging...

So, when I call the member function inputRPN, it compiles, but seg faults after printing "here" but doesn't print "and here". It also prints all the statements in the end of createExpressionTree. I just can't figure out why 'valid = createExpressionTree(expression);' would cause a seg fault if createExpressionTree is running fine... Let me know if you have any ideas and I'd owe you my first born child if you figure it out;)

int arithmetic_expression::inputRPN(string inputString)
{
int valid;
vector<string> expression = parse_string(inputString);
cout << "here"; //prints this
valid = createExpressionTree(expression);
cout << "and here"; //doesn't print this
if (!valid) {
topPtr = NULL;
}
cout << "--------value of valid is:" << valid;
cout << "--------value of topPtr is:" << topPtr->Op;
return(0);

}

int arithmetic_expression::createExpressionTree(vector<string> expression)
{
//TODO: ADD CODE HERE
// Create the expression tree from the RPN expression in vector<string> expression;

TreeNode newNode;
TreeNodePtr newNodePtr, currentPtr;
TreeNodePtr topPtr = NULL;
stack<TreeNodePtr> mystack;
int i;


for(i=0; i<expression.size(); i++)
{
if(!char_is_operand(expression[i])) //if element is an operand
{
//create new node and initialize leaves
newNodePtr = new TreeNode;
newNodePtr->Op = expression[i];
newNodePtr->rightPtr = NULL;
newNodePtr->leftPtr = NULL;
mystack.push(newNodePtr);
} //end if
else //if element is an operator
{
//create new node and initialize leaves
newNodePtr = new TreeNode;
newNodePtr->Op = expression[i];
// if element is an operator, place it in right leaf
if(char_is_operand((mystack.top()->Op)))
{
newNodePtr->rightPtr = mystack.top();
mystack.pop();
newNodePtr->leftPtr = mystack.top();
mystack.pop();
} //end if
else
{
newNodePtr->leftPtr = mystack.top();
mystack.pop();
newNodePtr->rightPtr = mystack.top();
mystack.pop();
} //end else
mystack.push(newNodePtr);
} //end else
} //end for

topPtr = mystack.top(); //initialize topPtr

//for debugging
for(currentPtr = topPtr; currentPtr!= NULL; currentPtr = currentPtr->rightPtr)
{
cout << currentPtr->Op;
cout << currentPtr->rightPtr->Op;
cout << currentPtr->leftPtr->Op;
}

return 0;
}
Last edited on
First off, when posting code, please use the code tags, shown as "<>" in the Format selection box. It will make it easier for us to read your code, and it provides line numbers for reference.

The problem is not where you think it is. The "and here" is actually being printed, but it never makes it you your console because you never flush the stdout buffer. The endl manipulator would flush the buffer as would the flush function, but since you did not call either, nothing got dumped to the console.

The problem is actually here:

1
2
3
4
5
if (!valid) {
     topPtr = NULL;
}
cout << "--------value of valid is:" << valid;
cout << "--------value of topPtr is:" << topPtr->Op;


Because createExpressionTree returns 0, valid has the value 0. Thus, !valid is true, and topPtr is set to NULL. When you dereference topPtr in my line 5 the program crashes, and the contents of stdout that were not flushed are lost.
Topic archived. No new replies allowed.