3 ms·
There's more discussion about this on this reddit thread: http://www.reddit.com/r/programming/comments/x1ql4/undefined_behavior_in_c_contest_winners/ http://www
by hackcasual 12y ago
There's more discussion about this on this reddit thread: http://www.reddit.com/r/programming/comments/x1ql4/undefined_behavior_in_c_contest_winners/ http://www.reddit.com/r/programming/comments/x1ql4/undefined...
The problem is that realloc builtin's return type is internally flagged as noalias, making clang think it was safe to use constant propagation for the printf. I guess LLVM doesn't take the equality check into account.
> 1. Breaking reflexivity is not OK. Ever. a==a should hold.
This reminds me of the "proof" that 1=2. Once an undefined operation is introduced, the end result means nothing.
> 2. The compiler knows that this is undefined behavior, and uses this to "optimize" to a wrong result.
Maybe, maybe not. It's not a requirement that a compiler even recognizes that undefined behavior happens. In many cases behavior is undefined simply because it would be too expensive or impractical for the compiler to deal with that behavior.