From 73ff58d6d353225d0b169337496c570f32b7e924 Mon Sep 17 00:00:00 2001 From: Frederik Beimgraben Date: Mon, 13 Jan 2025 02:58:08 +0100 Subject: [PATCH] Fix memory leak in calculateMedian --- MMS24-25.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/MMS24-25.c b/MMS24-25.c index d158337..0c61265 100644 --- a/MMS24-25.c +++ b/MMS24-25.c @@ -715,13 +715,17 @@ double calculateMedian(double *pdIn, int iLen) { // Sort the array using stdlib's qsort function qsort(pdInSorted, iLen, sizeof(double), compareDoubles); + double median; + if (iLen % 2 == 0) { - return (pdInSorted[iLen/2 - 1] + pdInSorted[iLen/2]) / 2; + median = (pdInSorted[iLen/2 - 1] + pdInSorted[iLen/2]) / 2; } else { - return pdInSorted[iLen/2]; + median = pdInSorted[iLen/2]; } free(pdInSorted); + + return median; } /*