From 9a2663c7a4f0263586f81295194c1e1e82dc857a Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Sun, 22 May 2022 15:26:17 +0200 Subject: [PATCH] replaced malloc in loop with realloc when I was coding first version of WHY2 I didn't know there's something like it lulw --- src/lib/encrypter.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/lib/encrypter.c b/src/lib/encrypter.c index 56ae472..b7bbb79 100644 --- a/src/lib/encrypter.c +++ b/src/lib/encrypter.c @@ -84,7 +84,15 @@ outputFlags encryptText(char *text, char *keyNew, inputFlags flags) //LOAD returningText for (int i = 0; i < strlen(text); i++) { - textBuffer = malloc(sizeof(int) * countIntLength(textKeyChain[i])); + numberBuffer = sizeof(int) * countIntLength(textKeyChain[i]); + + if (i != 0) + { + textBuffer = realloc(textBuffer, numberBuffer); + } else + { + textBuffer = malloc(numberBuffer); + } sprintf(textBuffer, "%d", textKeyChain[i]); @@ -94,8 +102,6 @@ outputFlags encryptText(char *text, char *keyNew, inputFlags flags) { strcat(returningText, ENCRYPTION_SEPARATOR_STRING); } - - free(textBuffer); } //LOAD output @@ -108,6 +114,7 @@ outputFlags encryptText(char *text, char *keyNew, inputFlags flags) //DEALLOCATION fclose(fileBuffer); free(textKeyChain); + free(textBuffer); return output; }