Undefined Isn’t Unpredictable
os2museum.com
os2museum.com
Once something has been declared undefined, there is a barrier to changing it to defined, just as much as changing it in the opposite direction.
I vaguely recall a recent C or C++ standard declaring something UB that wasn't UB before. Kinda hard to search for though, so the exact case so far eludes me.
c=-1;
ambiguously being either: c = -1; // c becomes -1
or c =- 1; // c decremented by 1It's:
c -= 1; // c decremented by 1
Notice the order is different.Compound assignment operators of the form =op (such as =-) were changed to the form op= (that is, -=) to remove the semantic ambiguity created by constructs such as i=-10, which had been interpreted as i =- 10 (decrement i by 10) instead of the possibly intended i = -10 (let i be −10).
https://en.wikipedia.org/wiki/C_(programming_language)#K&R_C
Perhaps with out of order CPUs relying on something that is undefined could change based on adjacent instructions also?
But UB in a language with an optimizer is a bigger gamble. Even changes to code outside of your UB-tainted function may trip inlining and hot code heuristics, which may make more or less of the code visible to the optimizer, which may make it take advantage of different "undefined" assumptions.
(Though comments at the bottom do point out that some of them are situations like a floating bus, where you really do get unpredictable results.)
See also: arbitrary contra random.