I'm confused as to why the following code still runs even though i have not allocated enough memory for the void* arr[]; i'm still able to write to those locations and not run into a segfault.
void** arr;
arr = realloc(NULL, 1); // 1 Byte allocated
arr[0] = (void *)1; // able to store a 8B pointer
arr = realloc(arr, 2); // 2 Bytes allocated
arr[1] = (void *)2; // able to store yet another 8B pointer
arr = realloc(arr, 4); // 4 Bytes allocated
arr[2] = (void *)3; // able to store yet another 8B pointer
arr[3] = (void *)4; // able to store yet another 8B pointer
// Error
arr = realloc(arr, 8); // realloc():invalid next size thrown presumaby
// because we wrote into the space for malloc metadata
Also, looking at this
it seems that the first assignment a[0] = 1 takes up only the first 4B chunk even though void*s are 8Bytes. The rest of the assignments do take up 8Bs so i'm also curious why the first a[0] only takes up 4Bytes.
Thank you!