Let's say I have a function which accepts a pointer to a struct, which is defined like so
typedef struct Array {
int capacity;
int size;
void **items;
} Array;
This function will realloc the memory of array->items
to increase the size.
The function is given below
void doubleArray(Array *array) {
array->capacity *= 2;
void **items = realloc(array->items, array->capacity);
if(items == NULL) {
exit(1);
}
array->items = items;
}
What I am have difficulty understanding is, if I assign void **items
to the result of realloc, and realloc returns a new memory address because the previous buffer has been overwritten, does array->items
get assigned correctly to the new value even after returning from the function?
Or will the fact that void **items
is defined within the local scope of the function doubleArray
mean that array->items
becomes a dangling pointer because it is not correctly re-assigned because the reference to void **items
is deleted once the function exits?