non-deterministic programs through unspecified behavior

C defines different types of behavior for certain features:

  1. deterministic behavior, most features such as the ! operator
  2. unspecified behavior, where the compiler has a choice on how to implement a feature, e.g the evaluation order of subexpressions. Here, I also include “needs not to” behavior, where it seems that it has been forgotten to mention this explicitly, e.g if two const qualified compound literals of the same scope with the same value are folded into one object
  3. implementation defined behavior, behavior unspecified by the standard but for which an implementation has to document its choices, e.g the greatest value of an int.
  4. undefined behavior, the compiler is allowed to eat your hard disk for breakfast, such as when accessing an array element out of bounds

Towards its end, the C standard has long list for the later three types.

Where most C programmers will know about the possible pitfalls of undefined behavior, it seems that unspecified behavior has a lot less attention although it might have severe consequences, too.
Continue reading “non-deterministic programs through unspecified behavior”