3 ms·
The light side of undefined behavior is not optimizations but extensions. Undefined behavior is necessary so that a language can have certain kinds of conformin
by kazinator 12y ago
The light side of undefined behavior is not optimizations but extensions. Undefined behavior is necessary so that a language can have certain kinds of conforming extensions. The language standard cannot define every possible program, because programs can do platform-specific things. For instance, a program might need to map some hardware to memory and access registers.
"Undefined behavior" in the ISO C sense only means that the ISO C standard doesn't provide requirements about how an implementation should react to a given situation. The implementation can still provide its own requirements, documented and all. ISO C undefined behavior can still be "GNU C defined" or "glibc defined".
Using some unknown conversion specifier in printf is undefined behavior according to ISO C, but the one being used could be documented by the given C library as an extension.
Using undefined behavior to make assumptions like that X < X + 1 is always true (and can be optimized away) is not a good example of undefined behavior being useful or beneficial. If the programmer wrote that, it was probably with the expectation that X + 1 could be less than X due to particular overflow semantics. Or maybe even X was once unsigned and someone changed it without considering that expression. This deserves a warning diagnostic like "warning: expression always true whenever its behavior is defined". And then, who cares how it is optimized; let the programmer fix it so the diagnostic goes away.
- chc 12y agoI thought this was the purpose of implementation-defined behavior rather than undefined behavior.
- kazinator 12y agoDifferent programming languages can possibly have different terminology for this. In C, "implementation-defined" behavior hinges around standard language features which have implementation-defined characteristics (which must be provided by an implementation). An example of such is the value of the CHAR_BIT macro in <limits.h> (how many bits in a byte) or the range of type int. It does not mean "anything defined by an implementation": that would be too obvious and clear a use of English.
- kruczek 12y agoUndefined behaviour means that anything can happen - also it may happen that particular implementation has such behaviour well defined ;)
- dllthomas 12y agoI had the same thought. The difference may be that the standard says implementations must define "implementation defined" behavior. On reflection, it would seem uncharacteristic for the standard to insist that undefined behavior never be defined by the implementation.
- marvy 12y agoUndefined means: the implementation can choose to define it, or can choose to leave it undefined. The standard prefers implementation defined when it's cheap, but some times it's not.
- comex 12y agoThe 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?