[musl] Re: realloci(): A realloc() variant that works in-place

Morten Welinder mwelinder@gmail.com
Fri Oct 31 23:46:23 GMT 2025


> The conclusion among C++ developers is that using the previous pointer in any
> way is UB. Therefore, you simply cannot know if the area was moved or not.

With a bit of effort you can travel from UB to implementation-defined.
The trick is to
use the pointer before the realloc.  Something like

      sprintf(buffer1, "%p", (void *)p);
      q = realloc(p, newsize);
      sprintf(buffer2, "%p", (void *)q);
      int moved = strcmp(buffer1, buffer2) != 0;

Clearly not UB.  I just can't convince myself that the standards
actually guarantee that a changed pointer doesn't have the same %p
representation.
I don't know of any platforms where a collision would actually happen, though.

M.


More information about the Libc-alpha mailing list