4 ms·
> The use of "undefined behavior" was and remains a mechanisms for achieving that goal. Is that really true? Couldn't the same goal be achieved by using implem
by moefh 7y ago
> The use of "undefined behavior" was and remains a mechanisms for achieving that goal.
Is that really true? Couldn't the same goal be achieved by using implementation-defined behavior instead?
I thought C keeps undefined behavior because it leaves the door open for optimizations. Otherwise (for example) int overflow could be left as implementation-defined behavior: almost everyone gets the expected behavior (two's complement wrap-around) and the 0.01% unlucky enough to be using a really old and weird processor gets some other behavior (one's complement wrap-around, traps or who knows what other kinds of processors have ever existed).
The reason why this is not done, in my understanding, is that int overflow (among other stuff) being left undefined means the compiler has more room for optimization.
- ncmncm 7y ago"Implementation-defined" creates a requirement on implementors to document a definition on physical paper. An implementer, though, may write down, "The behavior of this construct is undefined", and they have discharged that responsibility. Undefined really does mean not defined -- but, not defined in the Standard. Anybody is free to define anything the Standard doesn't, and a program may rely on that. #include <unistd.h> is UB, but your compiler uses the definition provided by the (rather, a) separate Posix Standard. Implementations do take advantage of explicit UB for optimization opportunities, but that is not its purpose.