Hacker News new | ask | show | jobs
by Noughmad 1696 days ago
> It's actually an interesting example where undefined behavior allowed compiler optimization

That is literally reason why any behavior is considered undefined. So that the compiler can skip checks to produce better optimized code.

2 comments

Actually, many instances of undefined behavior are born out of a desire for portability. Some processors will fault on both of those and the standard wants to be inclusive of those behaviors. Compilers happen to be able to use the definition of undefined behavior to also optimize code, but the concept is born out of practicality.
And so you get an optimal, but broken, program. This helps nobody.
It helps everyone whose input does not cause undefined behavior.
in most cases a program that invokes undefined behavior is already broken; it's just a question of how catastrophic the failure will be.