4 ms·
The reason why compilers are currently pretty bad at producing diagnostics in such cases is explained by a later post in the series (along with briefly explaini
by comex 12y ago
The reason why compilers are currently pretty bad at producing diagnostics in such cases is explained by a later post in the series (along with briefly explaining why the behavior is, in fact, useful):
http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_21.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-...
That it is useful (depending on your priorities) is confirmed simply by looking at performance. I don't have exact numbers, but -fwrapv is considered to generally make programs slower, albeit unevenly. Thus making such assumptions allows generating better code. This could be due to obviously wrong code like yours being skipped and thus producing an incorrect result faster - but such bugs are not that common in practice, and can be checked for using -ftrapv/UBSan/etc. Rather, the more assumptions the compiler can make, the more relatively complex things it can do...
edit: The benefit is more obvious with, say, strict aliasing, where it's extraordinarily useful to know some pointers can't alias, as it immediately allows removing redundant loads from the generated machine code in common cases like
y->foo = x->a;
// .. perhaps some other random stuff ..
if (x->a == 5)
and helps with things like loop vectorization.
- kazinator 12y agoYes; if you're coding C carefully and avoiding overflows, then you've already paid the price for that. Then you're wastefully paying again if the compiler caters to the nonportable approach you're not following.
- comex 12y agoI'm sorry, but I have no idea what you are trying to say. Can you clarify?