0

So I am creating a program that takes a postfix expression and converts it to infix using a binary tree. I keep getting a double free or corruption error in my main conversion functon here:

template <typename T>
bool Bet<T>::buildFromPostfix(const list<Token> & postfix)
{
     makeEmpty(root);
     vector<BinaryNode*> myVector;

    for(auto itr = postfix.begin(); itr != postfix.end(); itr++)
    {

        if(itr->getType() == 1 || itr->getType() == 2 || itr->getType() == 3 )
        {
            BinaryNode *newNode = new BinaryNode();
            newNode->element = itr->getValue();
            myVector.push_back(newNode);
        }
        else
        {
            BinaryNode *newNode = new BinaryNode();
            newNode->element = itr->getValue();

            newNode->right = myVector[myVector.size() - 1];
            myVector.pop_back();

            newNode->left = myVector[myVector.size() - 1];
            myVector.pop_back();

            myVector.push_back(newNode);

        }
        
            


    }

        if(myVector.size() != 1)
            {
                cout<<"Mismathcing operands and operators with: " <<myVector[0]->element.getValue()  <<endl;
                return false;
            }
        root = new BinaryNode(myVector[0]->element, myVector[0]->left,myVector[0]->right);

    return true;
} 

The error will only occur when the program goes into the if(myVector.size() != 1), the program will cout the statement but give the error right after it returns false.

Gilbert22
  • 47
  • 6
  • Did you try to debug your program? Since there are exactly 0 `delete` statements, I doubt you are getting double free anything. – Quimby Nov 04 '20 at 20:42
  • Is it possible that `myVector` is empty? – WBuck Nov 04 '20 at 20:48
  • Does this answer your question? [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Human-Compiler Nov 04 '20 at 20:57
  • Hm I did try and debug it for a few hours and the lack of delete statements is what confused me. MyVector is not empty because it prints out myVecotr[0] before it outputs the double free error. – Gilbert22 Nov 04 '20 at 21:45

0 Answers0