diff --git a/c-programming/experiments/reverse-ramdisk.c b/c-programming/experiments/reverse-ramdisk.c index 20bb066..5e57bde 100644 --- a/c-programming/experiments/reverse-ramdisk.c +++ b/c-programming/experiments/reverse-ramdisk.c @@ -13,7 +13,6 @@ * GCC/Clang/TCC: Compile with -DTEST to set macro TEST as defined, with * -DDEBUG to enable debug mode * - * To-Do: Test: Fix the bug with invalid next size to reallocate * To-Do: Test: Automate the test verification * * Author: Intel A80486DX2-66 @@ -109,7 +108,17 @@ int tf_alloc(size_t n, size_t type_size) { return -1; } - // Allocate memory for the TempFile struct + // Allocate/reallocate memory for all TempFiles + temp_files = temp_files == NULL ? + malloc(sizeof(TempFile)) + : + realloc(temp_files, (num_temp_files + 1) * sizeof(TempFile)); + if (temp_files == NULL) { + LINE_FAIL(-2); + return -1; + } + + // Allocate memory for this TempFile TempFile* temp_file = malloc(sizeof(TempFile)); if (temp_file == NULL) { LINE_FAIL(-2); @@ -122,16 +131,6 @@ int tf_alloc(size_t n, size_t type_size) { temp_file->file_path = strdup(file_path); temp_file->file = file; - // Allocate/reallocate memory for the temp_files structure - temp_files = temp_files == NULL ? - malloc(sizeof(TempFile)) - : - realloc(temp_files, num_temp_files * sizeof(TempFile)); - if (temp_files == NULL) { - LINE_FAIL(-2); - return -1; - } - // Add the temp file to the array temp_files[num_temp_files++] = *temp_file;