From 519d37f6bcfe0fd6997351685487ba5ee5a6af94 Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Fri, 30 Aug 2024 20:12:15 +0200 Subject: [PATCH] fix: remove memory issues (#2533) Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- sorting/counting_sort.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sorting/counting_sort.cpp b/sorting/counting_sort.cpp index 1fbfc0fa3a3..8daeeab6b43 100644 --- a/sorting/counting_sort.cpp +++ b/sorting/counting_sort.cpp @@ -27,6 +27,9 @@ int *Counting_Sort(int Arr[], int N) { int *Sorted_Arr = new int[N]; int *Count = new int[max - min + 1]; + for (int i = 0; i < max - min + 1; ++i) { + Count[i] = 0; + } for (int i = 0; i < N; i++) Count[Arr[i] - min]++; @@ -37,6 +40,7 @@ int *Counting_Sort(int Arr[], int N) { Count[Arr[i] - min]--; } + delete[] Count; return Sorted_Arr; } @@ -51,6 +55,7 @@ int main() { Sorted_Arr = Counting_Sort(Arr, N); cout << "\n\t Sorted Array = "; Print(Sorted_Arr, N); + delete[] Sorted_Arr; cout << endl; return 0;