I use this code in order to dynamically allocate more memory into my Struct array (mystructs
), by increasing the size of size
and reallocating memory:
int size = 1;
MyStruct *mystructs = NULL;
MyStruct *tmp = NULL;
tmp = realloc(mystructs, sizeof(MyStruct) * size);
mystructs = tmp;
My question is that, even if i use size = 1
it still allocates way more memory than needed for that size, because when i start printing out struct values with printf(), then i can usually print out a few hundred array elements although it is supposed to contain only 1 element. Printing out means calling something like: printf("%d", mystructs[i].value);
.
Why can i
be a value of about a few hundred, before it finally segfaults, because i accessed memory i wasn't supposed to ?