The function may move the memory block to a new location, in which case the new location is returned.
For example I have a pointer to an array:
int *arr; // somewhere next it initialized, filled with elements and etc
Somewhere I need to:
void* location = realloc(arr, NEW_SIZE);
What will happen with old memory block place?
If realloc return pointer that not math to arr, should i use next code?:
delete arr;
arr = (int*)location;
What will happen with old memory block place?
This question was not yet answered. The answer is simple, it is easy to check in your code. When realloc
returns the same address (usually if you reduce the block size), calling free
with old address produce no error, which is true since it is also a new address. When it returns different address, calling free
with old address fails.
So, it does free the old block when it is reallocated:
void* location = realloc(arr, NEW_SIZE);
// do not free arr, it points to the same or already freed block