@mikeash isn't that a compiler bug?
-
-
-
@dudrenov No. That program invokes undefined behavior (dereferencing a pointer after freeing it) so the compiler can pretty much do whatever -
@mikeash ya, you are right -
@dudrenov It is really weird and unexpected to hit that undefined behavior when the two pointers are the same, but it is valid to do so. -
@mikeash ya it is weird, kind of disturbing even. -
@dudrenov A lot of this stuff makes more sense if you think of *ptr as a separate value the compiler can manage, not a dereference command.
-
-
-
@mikeash but isn’t the bug in the sample? p is invalid after the realloc? -
@jon_gary_rocks Yes. Dereferencing a pointer after you free it is undefined behavior.
-
-
-
@mikeash wow. So does the compiler replace the *p and *q passed to printf with the assigned values to avoid the cost of dereferencing them? -
@Randy_Becker Yep. It knows the values can't have changed in legal code, so it caches them.
-
-
@mikeash but yeah, p and q should not be equal. -
@mikeash *shudder* -
@mikeash that's insane! If things are getting that crazy I'm done programming…become an artist or something. That's not how computers work! -
@mikeash yawn, it’s a use-after-free bug that the static analyzer catches.
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.
mikeash
dudrenov
Jøn (null)
Randy Becker
Loren Brichter
Dad
aburgh