From 438d62dbcd97164227c5520f3b27f7bec9e073ca Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Wed, 3 Jan 2024 21:03:43 +0300 Subject: [PATCH] reverse-ramdisk.c: fix reallocation, reduce code --- c-programming/reverse-ramdisk.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/c-programming/reverse-ramdisk.c b/c-programming/reverse-ramdisk.c index 5cb1a0e..c8b0669 100644 --- a/c-programming/reverse-ramdisk.c +++ b/c-programming/reverse-ramdisk.c @@ -146,13 +146,13 @@ int tf_free(int ID) { for (int i = index; i < num_temp_files - 1; i++) temp_files[(size_t) i] = temp_files[(size_t) i + 1]; - num_temp_files--; - // Reallocate memory for the temp_files array - temp_files = realloc(temp_files, num_temp_files * sizeof(TempFile)); - if (temp_files == NULL) { - line_fail(-2); - return -1; + if (--num_temp_files > 0) { + if ((temp_files = realloc(temp_files, num_temp_files + * sizeof(TempFile))) == NULL) { + line_fail(-2); + return -1; + } } temp_files[index].locked = false;