From 3c5f3b20065f65add02cd94e9c35867fdad64b1e Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sun, 24 Dec 2017 10:22:12 +0100 Subject: [PATCH] change: realloc in main function --- discspan.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/discspan.c b/discspan.c index 68c2df0..e01c2a9 100644 --- a/discspan.c +++ b/discspan.c @@ -92,7 +92,6 @@ unsigned long fill_array_from_file(FILE *in, struct file_size *fs, } lines--; - fs = (struct file_size *)realloc(fs, lines * sizeof(struct file_size)); if(ignore != NULL) fclose(ignore); @@ -142,18 +141,17 @@ int splitter(struct file_size *input, const char *output, rest[restcount++] = input[i]; } } + restcount--; fclose(out); printHumanReadSize(output, splitgroesse); if(restcount > 0) { memcpy(input, rest, length * sizeof(struct file_size)); - input = (struct file_size *)realloc(input, restcount * sizeof(struct file_size)); } free(rest); - // -1 weil vor Schleifenende nochmal incrementiert wird - return restcount - 1; + return restcount; } int main(int argc, char *argv[]) @@ -202,6 +200,7 @@ int main(int argc, char *argv[]) int s_length = get_array_length(in); struct file_size *fs = (struct file_size *)malloc((s_length + 1) * sizeof(struct file_size)); s_length = fill_array_from_file(in, fs, split); + fs = (struct file_size *)realloc(fs, s_length * sizeof(struct file_size)); fclose(in); // sortieren @@ -212,6 +211,7 @@ int main(int argc, char *argv[]) while(s_length > 0) { sprintf(outname, "%s%03d", output, num++); s_length = splitter(fs, outname, split, s_length); + fs = (struct file_size *)realloc(fs, s_length * sizeof(struct file_size)); } free(fs); -- 2.39.5