Set initial capacity to 50 chars.

modified:   src/nms.c
This commit is contained in:
Brian Barto 2017-09-20 09:00:38 -04:00
parent 39c02de520
commit 30bd00f949

View File

@ -12,10 +12,11 @@
#include "nmseffect.h" #include "nmseffect.h"
#define VERSION "0.3.3" #define VERSION "0.3.3"
#define INITIAL_CAPACITY 50
#define INPUT_GROWTH_FACTOR 2 #define INPUT_GROWTH_FACTOR 2
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int c, o, inSize = 0, inCapacity = 0; int c, o, inSize = 0, inCapacity = INITIAL_CAPACITY;
char *input = NULL; char *input = NULL;
// Processing command arguments // Processing command arguments
@ -45,11 +46,16 @@ int main(int argc, char *argv[]) {
} }
} }
if ((input = malloc(inCapacity + 1)) == NULL) {
fprintf (stderr, "Memory Allocation Error! Quitting...\n");
return 1;
}
// Geting input // Geting input
while ((c = getchar()) != EOF) { while ((c = getchar()) != EOF) {
++inSize; ++inSize;
if (inSize > inCapacity) { if (inSize > inCapacity) {
inCapacity = inCapacity == 0 ? INPUT_GROWTH_FACTOR : inCapacity * INPUT_GROWTH_FACTOR; inCapacity *= INPUT_GROWTH_FACTOR;
input = realloc(input, inCapacity + 1); input = realloc(input, inCapacity + 1);
} }
input[inSize - 1] = c; input[inSize - 1] = c;