From 30bd00f949d115d35a2da213b2c5236ba46633db Mon Sep 17 00:00:00 2001 From: Brian Barto Date: Wed, 20 Sep 2017 09:00:38 -0400 Subject: [PATCH] Set initial capacity to 50 chars. modified: src/nms.c --- src/nms.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/nms.c b/src/nms.c index c63cd44..ef2e038 100644 --- a/src/nms.c +++ b/src/nms.c @@ -12,10 +12,11 @@ #include "nmseffect.h" #define VERSION "0.3.3" +#define INITIAL_CAPACITY 50 #define INPUT_GROWTH_FACTOR 2 int main(int argc, char *argv[]) { - int c, o, inSize = 0, inCapacity = 0; + int c, o, inSize = 0, inCapacity = INITIAL_CAPACITY; char *input = NULL; // Processing command arguments @@ -44,12 +45,17 @@ int main(int argc, char *argv[]) { return 1; } } + + if ((input = malloc(inCapacity + 1)) == NULL) { + fprintf (stderr, "Memory Allocation Error! Quitting...\n"); + return 1; + } // Geting input while ((c = getchar()) != EOF) { ++inSize; if (inSize > inCapacity) { - inCapacity = inCapacity == 0 ? INPUT_GROWTH_FACTOR : inCapacity * INPUT_GROWTH_FACTOR; + inCapacity *= INPUT_GROWTH_FACTOR; input = realloc(input, inCapacity + 1); } input[inSize - 1] = c;