Note, this question is not asking if realloc()
invalidates pointers within the original block, but if it invalidates all the other pointers.
I am a bit confused about the nature of realloc()
, specifically if it moves any other memory.
For example:
void* ptr1 = malloc(2);
void* ptr2 = malloc(2);
....
ptr1 = realloc(ptr1, 3);
After this, can I guarantee that ptr2
points to the same data it did before the realloc()
call?
Yes,
ptr2
is unaffected byrealloc()
, it has no connection torealloc()
call whatsoever(as per the current code).However, FWIW, as per the man page of
realloc()
, (emphasis mine)ptr1
is likely to be changed.That said,
style is very dangerous. In case
realloc()
fails,and
but, as per your statement, in failure case the
NULL
return will overwrite the actual memory, losing the actual memory and creating memory leak.