Skip to content

Commit 8a36824

Browse files
authored
fix: remove memory leak iterative_factorial.cpp (#2535)
* fix: remove memory leak * tests: check properly if `math::iterativeFactorial` throws
1 parent 2dadbf7 commit 8a36824

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

math/iterative_factorial.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace math {
4646
*/
4747
uint64_t iterativeFactorial(uint8_t n) {
4848
if (n > 20) {
49-
throw new std::invalid_argument("Maximum n value is 20");
49+
throw std::invalid_argument("Maximum n value is 20");
5050
}
5151

5252
// 1 because it is the identity number of multiplication.
@@ -101,12 +101,14 @@ static void test() {
101101
std::cout << "Exception test \n"
102102
"Input: 21 \n"
103103
"Expected output: Exception thrown \n";
104+
105+
bool wasExceptionThrown = false;
104106
try {
105107
math::iterativeFactorial(21);
106-
} catch (std::invalid_argument* e) {
107-
std::cout << "Exception thrown successfully \nContent: " << e->what()
108-
<< "\n";
108+
} catch (const std::invalid_argument&) {
109+
wasExceptionThrown = true;
109110
}
111+
assert(wasExceptionThrown);
110112

111113
std::cout << "All tests have passed successfully.\n";
112114
}

0 commit comments

Comments
 (0)