From 4daed5ebe3e8eae184a96055974ac15a3c55507f Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Sun, 15 Apr 2012 00:26:12 +0200 Subject: Removed all memory leaks detected by valgrind --- main.cpp | 4 +-- parser.cpp | 86 +++++++++++++++++++++++++++++++------------------------------- parser.h | 4 +-- tree.cpp | 12 +++++++-- tree.h | 1 + 5 files changed, 58 insertions(+), 49 deletions(-) diff --git a/main.cpp b/main.cpp index 5044f18..d12119e 100644 --- a/main.cpp +++ b/main.cpp @@ -8,13 +8,13 @@ int main(int argc, char *argv[]) std::cin >> inputTerm; // Example: 2.5*(2+3-(3/2+1)) - Parser *parser = new Parser(); + Parser parser; try { typedef std::numeric_limits dbl; std::cout.precision(dbl::digits10); - std::cout << parser->calculate(inputTerm, false).result << std::endl; + std::cout << parser.calculate(inputTerm, false).result << std::endl; } catch ( exception &e ) { diff --git a/parser.cpp b/parser.cpp index 968053d..7ce5cab 100644 --- a/parser.cpp +++ b/parser.cpp @@ -24,14 +24,14 @@ int Parser::getPriority(char tmp) } } -vector* Parser::lexer(string term) +vector Parser::lexer(string term) { int priority = 0; int last_priority = 0; int level = 0; string tmp, tmp_num; - vector *output = new vector(); + vector output; string::iterator termIter; @@ -48,7 +48,7 @@ vector* Parser::lexer(string term) } else { if ( last_priority == -1 && level == 0 ) { - output->push_back( tmp_num ); + output.push_back( tmp_num ); tmp_num = ""; } @@ -65,7 +65,7 @@ vector* Parser::lexer(string term) level--; if ( level == 0 ) { - output->push_back( tmp ); + output.push_back( tmp ); tmp = ""; } else { @@ -78,7 +78,7 @@ vector* Parser::lexer(string term) string helper; helper = *termIter; - output->push_back( helper ); + output.push_back( helper ); } else { tmp += *termIter; @@ -92,7 +92,7 @@ vector* Parser::lexer(string term) } if ( last_priority == -1 ) { - output->push_back( tmp_num ); + output.push_back( tmp_num ); } else if ( last_priority != 90 ) { throw operator_exception(); @@ -105,95 +105,95 @@ vector* Parser::lexer(string term) return output; } -Node* Parser::buildTree(Tree **tree, string term) +Node* Parser::buildTree(Tree *tree, string term) { - stack *operandStack = new stack(); - stack *operatorStack = new stack(); + stack operandStack; + stack operatorStack; int priority; - vector *tmpLexer; + vector tmpLexer; - vector *lexerOutput = this->lexer(term); + vector lexerOutput = this->lexer(term); - for ( vector::iterator termIter = lexerOutput->begin(); termIter != lexerOutput->end(); termIter++ ) { + for ( vector::iterator termIter = lexerOutput.begin(); termIter != lexerOutput.end(); termIter++ ) { priority = this->getPriority( (*termIter)[0] ); if ( priority != -1 && (*termIter).size() == 1 ) { - if ( !operatorStack->empty() ) { - OperatorNode *lastNode = static_cast( operatorStack->top() ); + if ( !operatorStack.empty() ) { + OperatorNode *lastNode = static_cast( operatorStack.top() ); if ( this->getPriority( lastNode->function ) < priority ) { - operatorStack->push( - (*tree)->addOperator( NULL, (*termIter)[0] ) + operatorStack.push( + tree->addOperator( NULL, (*termIter)[0] ) ); } else { - Node *currOperator = operatorStack->top(); - operatorStack->pop(); + Node *currOperator = operatorStack.top(); + operatorStack.pop(); - currOperator->rightChild = operandStack->top(); - operandStack->pop(); + currOperator->rightChild = operandStack.top(); + operandStack.pop(); - currOperator->leftChild = operandStack->top(); - operandStack->pop(); + currOperator->leftChild = operandStack.top(); + operandStack.pop(); - operandStack->push( currOperator ); + operandStack.push( currOperator ); termIter--; } } else { - operatorStack->push( - (*tree)->addOperator( NULL, (*termIter)[0] ) + operatorStack.push( + tree->addOperator( NULL, (*termIter)[0] ) ); } } else { tmpLexer = this->lexer( *termIter ); - if ( tmpLexer->size() == 1 ) { - operandStack->push( - (*tree)->addOperand( NULL, + if ( tmpLexer.size() == 1 ) { + operandStack.push( + tree->addOperand( NULL, strtod( termIter->c_str(), NULL ) ) ); } - else if ( tmpLexer->size() > 1 ) { - operandStack->push( + else if ( tmpLexer.size() > 1 ) { + operandStack.push( buildTree(tree, *termIter) ); } } } - while ( !operatorStack->empty() ) { - OperatorNode *currOperator = static_cast( operatorStack->top() ); - operatorStack->pop(); + while ( !operatorStack.empty() ) { + OperatorNode *currOperator = static_cast( operatorStack.top() ); + operatorStack.pop(); - currOperator->rightChild = operandStack->top(); - operandStack->pop(); + currOperator->rightChild = operandStack.top(); + operandStack.pop(); - currOperator->leftChild = operandStack->top(); - operandStack->pop(); + currOperator->leftChild = operandStack.top(); + operandStack.pop(); - operandStack->push( currOperator ); + operandStack.push( currOperator ); } - return operandStack->top(); + return operandStack.top(); } ParserResult Parser::calculate(string term, bool getTreeAsDot) { - Tree *termTree = new Tree(); - termTree->root = buildTree(&termTree, term); + Tree termTree; + termTree.root = this->buildTree(&termTree, term); ParserResult returnVal; - returnVal.result = termTree->root->solve(); + returnVal.result = termTree.root->solve(); if ( getTreeAsDot ) { - returnVal.tree = termTree->print(term); + returnVal.tree = termTree.print(term); } return returnVal; diff --git a/parser.h b/parser.h index 0aeaae9..e9cdde1 100644 --- a/parser.h +++ b/parser.h @@ -16,8 +16,8 @@ class Parser private: int getPriority(char); - vector* lexer(string); - Node* buildTree(Tree**, string); + vector lexer(string); + Node* buildTree(Tree*, string); }; class parenthese_exception: public exception diff --git a/tree.cpp b/tree.cpp index 0ef3a39..29dbab1 100644 --- a/tree.cpp +++ b/tree.cpp @@ -69,11 +69,20 @@ Tree::Tree() this->nodeCollection = new vector(); } +Tree::~Tree() +{ + for ( vector::iterator it = this->nodeCollection->begin(); it != this->nodeCollection->end(); it++ ) + { + delete *it; + } + + delete this->nodeCollection; +} + Node* Tree::addOperand(Node **place, double value) { OperandNode *newNode = new OperandNode(); - newNode = new OperandNode(); newNode->value = value; this->nodeCollection->push_back( newNode ); @@ -89,7 +98,6 @@ Node* Tree::addOperator(Node **place, char oper) { OperatorNode *newNode = new OperatorNode(); - newNode = new OperatorNode(); newNode->function = oper; this->nodeCollection->push_back( newNode ); diff --git a/tree.h b/tree.h index b112dcb..f2a7017 100644 --- a/tree.h +++ b/tree.h @@ -47,6 +47,7 @@ class Tree { public: Tree(); + ~Tree(); Node *root; -- cgit v1.2.3