4 ms·
> The compiler is aware that realloc invalidates its pointer parameter. According to the standard, realloc doesn't invalidate its pointer parameter, since it c
by masklinn 11y ago
> The compiler is aware that realloc invalidates its pointer parameter.
According to the standard, realloc doesn't invalidate its pointer parameter, since it can return the existing pointer (if the resizing could be done in-place) and can not touch the existing pointer (if the reallocation fails).
- gpderetta 11y agoAccording to the latest C standard draft that I could find, the semantics of realloc are that it deallocates the old object and allocates a new one. Of course, as an intended optimization, the new pointer value might have the same value as the old one, but that's not part of the semantics. The only case in which the old pointer is not invalidated is if realloc fails. As far as I know, in practice, at least with GCC, but likely clang as well, the compiler doesn't tracks the invalidation, but it simply assumes that any non null pointer returned by realloc (or any allocation function in fact) does not alias any other existing pointer in the program.