On 9/1/21 5:21 PM, Michael Kerrisk (man-pages) wrote:
Obviously, '*ptr' is invalid after a successful realloc(). But why is 'ptr' invalid?
The C standard says that once a program has freed a non-null pointer, the program cannot look at the pointer's value any more. It can't copy the value to another pointer, and it can't even test whether the value is null. A debugging implementation can trap any use of the pointer's value.
This rule is not an issue for production uses of glibc realloc. However, it could at least in theory affect debugging implementations like gcc -fsanitize=address, because the rule can help catch bugs in programs. And I vaguely recall talk that a few non-glibc platforms (IBM i, perhaps?) enforce the rule.