Skip to content

Commit

Permalink
Fix memory leaks (krahets#433)
Browse files Browse the repository at this point in the history
* fix(codes/cpp): Memory leak fix: the space was not freed when pop removed the element.

* fix(codes/cpp): Fix access error when printArray(arr, 0)

* fix(codes/cpp): Fix memory leaks: replace pointers with local variables, no need to manage memory
  • Loading branch information
Gonglja authored Mar 20, 2023
1 parent c837882 commit 0659c54
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions codes/cpp/include/PrintUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,13 @@ class PrintUtil {
static void printArray(T* arr, int n)
{
cout << "[";
for (size_t i = 0; i < n - 1; i++) {
for (int i = 0; i < n - 1; i++) {
cout << arr[i] << ", ";
}
cout << arr[n - 1] << "]" << '\n';
if (n>=1)
cout << arr[n - 1] << "]" << endl;
else
cout << "]" << endl;
}

/**
Expand Down Expand Up @@ -206,31 +209,31 @@ class PrintUtil {
}

string prev_str = " ";
Trunk *trunk = new Trunk(prev, prev_str);
Trunk trunk(prev, prev_str);

printTree(root->right, trunk, true);
printTree(root->right, &trunk, true);

if (!prev) {
trunk->str = "———";
trunk.str = "———";
}
else if (isLeft) {
trunk->str = "/———";
trunk.str = "/———";
prev_str = " |";
}
else {
trunk->str = "\\———";
trunk.str = "\\———";
prev->str = prev_str;
}

showTrunks(trunk);
showTrunks(&trunk);
cout << " " << root->val << endl;

if (prev) {
prev->str = prev_str;
}
trunk->str = " |";
trunk.str = " |";

printTree(root->left, trunk, false);
printTree(root->left, &trunk, false);
}

/**
Expand Down

0 comments on commit 0659c54

Please sign in to comment.