|
|
|
|
|
by anttihaapala
719 days ago
|
|
No, it is not, and it has never been OK for as long as C has been standardized by ANSI or ISO. C89 says that "The value of a pointer that refers to freed space is indeterminate." and that behavior is undefined "upon use ... of indeterminately-valued objects", hence a compiled program can e.g. behave as if `new_pointer == old_pointer` even though the object was relocated in memory. |
|
Using clang, program #1:
prints out: Program #2: (only difference is the extra printf after malloc) prints out: So if we print out the pointer before reallocation then they're not equal, but if we don't then they are equal.Funny enough, "-fsanitize=undefined" doesn't seem to detect this. Neither does "-fsanitize=address" (but with ASAN the results are now consistent and in both cases compare to not equal).