4 ms·
John Regehr is an expert in undefined behavior in C compilers, so rather than assume his understanding is mistaken, I would first assume mine is. He is not acc
by scott_s 14y ago
John Regehr is an expert in undefined behavior in C compilers, so rather than assume his understanding is mistaken, I would first assume mine is.
He is not accusing the compilers of executing undefined behavior. He is accusing them of exploiting undefined behavior to "optimize" code.
His points are:
1. Current compilers will perform optimizations based on undefined behavior. That is, during the optimization phase, they will say "Aha! That's not defined! I'll instead do this much, much faster thing as an optimization."
2. Lots of real code has undefined behavior.
3. The programs that result from these codes still produce the answers we expect, mostly.
4. If we take point 1 to its logical conclusion, then point 2 means that point 3 will no longer be true. That is, if compilers aggressively and thoroughly sought out to find all instances of undefined behavior in C code, and just optimized it away, many (most?) programs would cease to do much at all.
- Elrac 14y agoIronically enough, point #4 sounds very similar to how Vigil claims to work.
- scott_s 14y agoI don't think it's irony, I think that's the very reason that qznc linked to it.
- tikhonj 14y agoThat sounds like a problem with the C standard rather than any compiler.
- qznc 14y agoIt is less of a "problem" with the standard, but more of a "design decision". Performance is more important to C-people. For example, the result of x+y is undefined for signed integers in C, because we want it to compile it to a minimum number of instructions on every architecture. No instructions to check for overflow and branching afterwards. No error correction instructions on architectures where semantics differ to the language semantics.
- bcoates 14y agoYou're exaggerating a little, x+y is defined over signed ints as long as as it doesn't over/underflow. If it does overflow, it's either a target-specific hand optimization or a programmer error, but that varies on a program-by-program basis, hence GCC having -fwrapv, -ftrapv, and the default of "I don't do that, optimize accordingly".
- bcoates 14y agoHmm, is there some sort of reverse Poe's law where you confuse parody for a good idea? I may have entirely misinterpreted this thread.