From 1b3a1ca91ee4696f8d51f8cd5ffc223eee37cf5d Mon Sep 17 00:00:00 2001 From: Enzo Veroneze <78752573+enzoveroneze@users.noreply.github.com> Date: Thu, 10 Nov 2022 02:19:01 -0300 Subject: [PATCH] fix: remove double/unintended `free` (#1143) The call to realloc() already frees the previously allocated memory if necessary. By the man page of realloc(): "f the area pointed to was moved, a free(ptr) is done.", and free(): "If free(ptr) has already been called before, undefined behavior occurs.". --- data_structures/dynamic_array/dynamic_array.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/data_structures/dynamic_array/dynamic_array.c b/data_structures/dynamic_array/dynamic_array.c index 7ecf359c..19631cb1 100644 --- a/data_structures/dynamic_array/dynamic_array.c +++ b/data_structures/dynamic_array/dynamic_array.c @@ -18,7 +18,6 @@ void *add(dynamic_array_t *da, const void *value) { void **newItems = realloc(da->items, (da->capacity <<= 1) * sizeof(void **)); - free(da->items); da->items = newItems; } @@ -79,4 +78,4 @@ void *retrive_copy_of_value(const void *value) memcpy(value_copy, value, sizeof(void *)); return value_copy; -} \ No newline at end of file +}