From 136c22421e50112f5c316150765a068cba59cd20 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Fri, 17 Nov 2023 13:53:25 -0700 Subject: [PATCH] Drastically reduce use of heap memory and memory leaks --- legacy/Evaluator.cpp | 20 ++++++------ legacy/Evaluator.hpp | 2 +- legacy/RationalNumber.cpp | 35 ++++++++++----------- legacy/RationalNumber.h | 13 ++++---- legacy/StringTokenizer.cpp | 63 +++++++++++-------------------------- legacy/StringTokenizer.h | 25 +++++++-------- legacy/infiz.cpp | 10 +++--- legacy/infiz.ncb | Bin 74752 -> 74752 bytes legacy/infiz.opt | Bin 61952 -> 61952 bytes legacy/infiz.plg | 8 ++--- legacy/tests.cpp | 28 +++++------------ legacy/tests/tests.plg | 38 ---------------------- 12 files changed, 78 insertions(+), 164 deletions(-) diff --git a/legacy/Evaluator.cpp b/legacy/Evaluator.cpp index 2a38818..0ee0541 100644 --- a/legacy/Evaluator.cpp +++ b/legacy/Evaluator.cpp @@ -69,7 +69,7 @@ void evaluateStacks(Stack * numbers, Stack * operators, int num) { operand2 = (RationalNumber *)numbers->pop(); operand1 = (RationalNumber *)numbers->pop(); - operand1->add(operand2); + operand1->add(*operand2); delete operand2; numbers->push(operand1); break; @@ -87,7 +87,7 @@ void evaluateStacks(Stack * numbers, Stack * operators, int num) { operand2 = (RationalNumber *)numbers->pop(); operand1 = (RationalNumber *)numbers->pop(); - operand1->multiply(operand2); + operand1->multiply(*operand2); delete operand2; numbers->push(operand1); break; @@ -97,7 +97,7 @@ void evaluateStacks(Stack * numbers, Stack * operators, int num) { operand2 = (RationalNumber *)numbers->pop(); operand1 = (RationalNumber *)numbers->pop(); - operand1->divide(operand2); + operand1->divide(*operand2); delete operand2; numbers->push(operand1); break; @@ -112,7 +112,7 @@ void evaluateStacks(Stack * numbers, Stack * operators, int num) { -RationalNumber * evaluateExpression(StringTokenizer * st) { +RationalNumber evaluateExpression(StringTokenizer & st) { Stack * operators = new Stack(); Stack * numbers = new Stack(); @@ -120,15 +120,13 @@ RationalNumber * evaluateExpression(StringTokenizer * st) { int op; - char * next; + while (st.hasMoreTokens()) { - while (st->hasMoreTokens()) { - - next = st->nextToken(); + std::string next = st.nextToken(); int * value = new int; - if (next != NULL) { + if (!next.empty()) { switch(next[0]) { case '+': *value = PLUS_SIGN; @@ -156,7 +154,7 @@ RationalNumber * evaluateExpression(StringTokenizer * st) { break; default: - *value = atoi(next); + *value = atoi(next.c_str()); op = FALSE; numbers->push(new RationalNumber(*value, 1)); break; @@ -195,6 +193,6 @@ RationalNumber * evaluateExpression(StringTokenizer * st) { if (operators->peek() != NULL) evaluateStacks(numbers, operators, 0); - return (RationalNumber *)numbers->peek(); + return *(RationalNumber *)numbers->peek(); } diff --git a/legacy/Evaluator.hpp b/legacy/Evaluator.hpp index df18c14..5766bbf 100644 --- a/legacy/Evaluator.hpp +++ b/legacy/Evaluator.hpp @@ -6,7 +6,7 @@ #include "StringTokenizer.h" int precedence(int op); -RationalNumber * evaluateExpression(StringTokenizer * st); +RationalNumber evaluateExpression(StringTokenizer &st); void evaluateStacks(Stack * numbers, Stack * operators, int num); #endif \ No newline at end of file diff --git a/legacy/RationalNumber.cpp b/legacy/RationalNumber.cpp index 9bbb605..6e391c0 100644 --- a/legacy/RationalNumber.cpp +++ b/legacy/RationalNumber.cpp @@ -7,39 +7,36 @@ RationalNumber::RationalNumber(int num, int den) { denominator = den; } -RationalNumber::~RationalNumber() { +void RationalNumber::divide(const RationalNumber & rn) { + denominator *= rn.getNumerator(); + numerator *= rn.getDenominator(); } -void RationalNumber::divide(RationalNumber * rn) { - denominator *= rn->getNumerator(); - numerator *= rn->getDenominator(); +void RationalNumber::multiply(const RationalNumber & rn) { + numerator *= rn.getNumerator(); + denominator *= rn.getDenominator(); } -void RationalNumber::multiply(RationalNumber * rn) { - numerator *= rn->getNumerator(); - denominator *= rn->getDenominator(); -} - -void RationalNumber::add(RationalNumber * rn) { - int denom = denominator * rn->getDenominator(); - numerator *= rn->getDenominator(); - int numer = numerator + (rn->getNumerator() * denominator); +void RationalNumber::add(const RationalNumber &rn) { + int denom = denominator * rn.getDenominator(); + numerator *= rn.getDenominator(); + int numer = numerator + (rn.getNumerator() * denominator); numerator = numer; denominator = denom; } -void RationalNumber::subtract(RationalNumber * rn) { - int denom = denominator * rn->getDenominator(); +void RationalNumber::subtract(const RationalNumber &rn) { + int denom = denominator * rn.getDenominator(); - numerator *= rn->getDenominator(); - int numer = numerator - (rn->getNumerator() * denominator); + numerator *= rn.getDenominator(); + int numer = numerator - (rn.getNumerator() * denominator); numerator = numer; denominator = denom; } -int RationalNumber::getDenominator() { +int RationalNumber::getDenominator() const { return denominator; } @@ -47,7 +44,7 @@ void RationalNumber::negate() { numerator*=-1; } -int RationalNumber::getNumerator() { +int RationalNumber::getNumerator() const { return numerator; } diff --git a/legacy/RationalNumber.h b/legacy/RationalNumber.h index 3c7247b..24940d1 100644 --- a/legacy/RationalNumber.h +++ b/legacy/RationalNumber.h @@ -11,15 +11,14 @@ class RationalNumber { public: RationalNumber(int num, int den); - ~RationalNumber(); - void divide(RationalNumber * rn); - void multiply(RationalNumber * rn); - void add(RationalNumber * rn); - void subtract(RationalNumber * rn); + void divide(const RationalNumber & rn); + void multiply(const RationalNumber & rn); + void add(const RationalNumber & rn); + void subtract(const RationalNumber & rn); void negate(); - int getNumerator(); - int getDenominator(); + int getNumerator() const; + int getDenominator() const; float getFloat(); diff --git a/legacy/StringTokenizer.cpp b/legacy/StringTokenizer.cpp index b31f314..94accca 100644 --- a/legacy/StringTokenizer.cpp +++ b/legacy/StringTokenizer.cpp @@ -3,54 +3,38 @@ #include "StringTokenizer.h" #include -StringTokenizer::StringTokenizer(char * n_string) { - - string = new char[strlen(n_string) + 1]; - strcpy(string, n_string); - - stringLen = strlen(string); - - currentOffset = 0; - moreTokens = 1; -} - -StringTokenizer::~StringTokenizer() +StringTokenizer::StringTokenizer(const std::string &n_string) + : string(n_string), currentOffset(0), moreTokens(true) { - delete [] string; } -char * StringTokenizer::nextToken() -{ - char* toReturn; - - int cont = 1; + +std::string StringTokenizer::nextToken() +{ int endOfToken; - for (;(currentOffset= string.size()) + moreTokens = false; return toReturn; } -int StringTokenizer::hasMoreTokens() { +bool StringTokenizer::hasMoreTokens() { return moreTokens; } -int StringTokenizer::findTokenEnd(int start, char * string) { - +int StringTokenizer::findTokenEnd(int start, const std::string& string) { if ( isNumber(string[start]) ) { - for (; (currentOffset // StringTokenizer.h @@ -12,24 +13,22 @@ class StringTokenizer { public: - StringTokenizer(char * n_string); - ~StringTokenizer(); + StringTokenizer(const std::string &n_string); - int hasMoreTokens(); - - char * nextToken(); + bool hasMoreTokens(); + std::string nextToken(); private: + std::string string; int currentOffset; - int stringLen; - char * string; - int isNumber(char c); - int isOperator(char c); - int isWhiteSpace(char c); - char * getSubString(char * string, int start, int end); - int findTokenEnd(int start, char * string); - int moreTokens; + bool moreTokens; + + static bool isNumber(char c); + static bool isOperator(char c); + static bool isWhiteSpace(char c); + static std::string getSubString(const std::string &, int start, int end); + static int findTokenEnd(int start, const std::string &); }; #endif \ No newline at end of file diff --git a/legacy/infiz.cpp b/legacy/infiz.cpp index 69253f1..7b4af99 100644 --- a/legacy/infiz.cpp +++ b/legacy/infiz.cpp @@ -16,13 +16,13 @@ void main(int argc, char * args[]) { cin.getline(input,254, '\n'); while (cin) { - StringTokenizer * st = new StringTokenizer(input); - RationalNumber * answer = evaluateExpression(st); + StringTokenizer st(input); + RationalNumber answer = evaluateExpression(st); cout << "answer: "; - if (answer->getDenominator()==1) - cout <getNumerator()<getNumerator()<<'/'<getDenominator()<<" ("<getFloat()<<")"<mPUIl6QJ-x3xt3 zKeJS?-5E{fp<8n&I>SY-P^ru2(4tFkIu&l+8|9YUqKF3a-7*0RodzxeekO^i3OF6* zO~AQMqTPb-VvH8O!0o8-2fhM409*mNP-M!6ki1?WYTNJ%uG3TOd6K_!$$ zUV5C;X)ZlRU!w_+(sU}KR2oCsG?UV3ERCb_ltB|Hljdnzl?k%qqADoILug1*V0(n# z3)9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y>>|3bia6zcGNI5yirE_TX13a*{iP{FM_^U;d!!c-)7vspB|d~-y~ zZXUJb*e5dU|L%hh*8erRpY{LD9@GC1ACcNt2CC2hV(z1b{&)W25o6l|fi`2HuhaX` y$@^>~nz?nkl}9YmOmHS~J=nrSjA$mByfrd1(voQ+fk!fX=RY>?DKoHh2L1yxPYvV% diff --git a/legacy/infiz.plg b/legacy/infiz.plg index 2289607..9d7663c 100644 --- a/legacy/infiz.plg +++ b/legacy/infiz.plg @@ -6,19 +6,19 @@ --------------------Configuration: infiz - Win32 Debug--------------------

Command Lines

-Creating temporary file "C:\Users\Jason\AppData\Local\Temp\RSPB26E.tmp" with contents +Creating temporary file "C:\Users\Jason\AppData\Local\Temp\RSP51B6.tmp" with contents [ /nologo /MLd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Fp"Debug/infiz.pch" /YX /Fo"Debug/" /Fd"Debug/" /FD /c "C:\Users\Jason\Documents\GitHub\infiz\legacy\infiz.cpp" ] -Creating command line "cl.exe @C:\Users\Jason\AppData\Local\Temp\RSPB26E.tmp" -Creating temporary file "C:\Users\Jason\AppData\Local\Temp\RSPB26F.tmp" with contents +Creating command line "cl.exe @C:\Users\Jason\AppData\Local\Temp\RSP51B6.tmp" +Creating temporary file "C:\Users\Jason\AppData\Local\Temp\RSP51B7.tmp" with contents [ kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /incremental:yes /pdb:"Debug/infiz.pdb" /debug /machine:I386 /out:"Debug/infiz.exe" /pdbtype:sept .\Debug\infiz.obj .\InfixEvaluator\Debug\InfixEvaluator.lib ] -Creating command line "link.exe @C:\Users\Jason\AppData\Local\Temp\RSPB26F.tmp" +Creating command line "link.exe @C:\Users\Jason\AppData\Local\Temp\RSP51B7.tmp"

Output Window

Compiling... infiz.cpp diff --git a/legacy/tests.cpp b/legacy/tests.cpp index 54af179..4749c0d 100644 --- a/legacy/tests.cpp +++ b/legacy/tests.cpp @@ -12,27 +12,13 @@ #include "Evaluator.hpp" -void run_test(const char *input, RationalNumber expected) +void run_test(const std::string &input, RationalNumber expected) { - // Possible UB, const_cast, but we need to capture the state - // then fix later! - StringTokenizer st(const_cast(input)); - - // these will have to change once I make the code correct without heap everywhere - - // why you used the heap: - // * stack was too small! (not right!) - // * that's just how we did it back then! (but that's still bad practice even then!) - // - // actual reasons: - // * I didn't understand copy constructors - // * I didn't use stdlib - // * I was learning Java also at that time - // * I don't recall professors ever discussing memory leaks or efficiency! - - RationalNumber *result = evaluateExpression(&st); - if (result->getDenominator() != expected.getDenominator() || - result->getNumerator() != expected.getNumerator()) { + StringTokenizer st(input); + RationalNumber result = evaluateExpression(st); + + if (result.getDenominator() != expected.getDenominator() || + result.getNumerator() != expected.getNumerator()) { std::cout << "FAIL: "; } else { std::cout << "PASS: "; @@ -40,7 +26,7 @@ void run_test(const char *input, RationalNumber expected) std::cout << "input: " << input << " expected: " << expected.getNumerator() << '/' << expected.getDenominator() - << " got: " << result->getNumerator() << '/' << result->getDenominator() << '\n'; + << " got: " << result.getNumerator() << '/' << result.getDenominator() << '\n'; } int main() { diff --git a/legacy/tests/tests.plg b/legacy/tests/tests.plg index ad73ca0..9cdf2d3 100644 --- a/legacy/tests/tests.plg +++ b/legacy/tests/tests.plg @@ -3,47 +3,9 @@
 

Build Log

---------------------Configuration: InfixEvaluator - Win32 Debug-------------------- -

-

Command Lines

-Creating temporary file "C:\Users\Jason\AppData\Local\Temp\RSP31DC.tmp" with contents -[ -/nologo /MLd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /Fp"Debug/InfixEvaluator.pch" /YX /Fo"Debug/" /Fd"Debug/" /FD /GZ /c -"C:\Users\Jason\Documents\GitHub\infiz\legacy\Evaluator.cpp" -"C:\Users\Jason\Documents\GitHub\infiz\legacy\RationalNumber.cpp" -"C:\Users\Jason\Documents\GitHub\infiz\legacy\Stack.cpp" -"C:\Users\Jason\Documents\GitHub\infiz\legacy\StringTokenizer.cpp" -] -Creating command line "cl.exe @C:\Users\Jason\AppData\Local\Temp\RSP31DC.tmp" -Creating command line "link.exe -lib /nologo /out:"Debug\InfixEvaluator.lib" .\Debug\Evaluator.obj .\Debug\RationalNumber.obj .\Debug\Stack.obj .\Debug\StringTokenizer.obj " -

Output Window

-Compiling... -Evaluator.cpp -RationalNumber.cpp -Stack.cpp -StringTokenizer.cpp -Creating library... -

--------------------Configuration: tests - Win32 Debug--------------------

Command Lines

-Creating temporary file "C:\Users\Jason\AppData\Local\Temp\RSP3306.tmp" with contents -[ -/nologo /MLd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Fp"Debug/tests.pch" /YX /Fo"Debug/" /Fd"Debug/" /FD /GZ /c -"C:\Users\Jason\Documents\GitHub\infiz\legacy\tests.cpp" -] -Creating command line "cl.exe @C:\Users\Jason\AppData\Local\Temp\RSP3306.tmp" -Creating temporary file "C:\Users\Jason\AppData\Local\Temp\RSP3307.tmp" with contents -[ -kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /incremental:yes /pdb:"Debug/tests.pdb" /debug /machine:I386 /out:"Debug/tests.exe" /pdbtype:sept -.\Debug\tests.obj -\Users\Jason\Documents\GitHub\infiz\legacy\InfixEvaluator\Debug\InfixEvaluator.lib -] -Creating command line "link.exe @C:\Users\Jason\AppData\Local\Temp\RSP3307.tmp" -

Output Window

-Compiling... -tests.cpp -Linking...