Undefined behaviour

From Free net encyclopedia

In computer science, undefined behavior is a feature of some programming languages — most famously C. In these languages, to simplify the specification and allow some flexibility in implementation, the specification leaves the results of certain operations specifically undefined.

For example, in C the use of any variable before it has been initialized yields undefined behavior. This specifically frees the compiler to do whatever is easiest or most efficient, should such a program be entered. In general, any behaviour whatsoever is valid after a situation resulting in undefined behavior has been encountered. In particular, it is never required that the compiler diagnose undefined behavior — therefore, programs invoking undefined behavior may appear to compile and even run without errors at first, only to fail on another system, or even on another date!

On the other hand, in some languages (including C), even the compiler is not bound to behave in a sensible manner once undefined behavior has been invoked. In an extreme sense, it would be permitted by the specification for the compiler to launch a game of Space Invaders when it handles undefined behavior.

One famous piece of hacker lore is the original behavior of the GCC C compiler when given a program containing the non-standard #pragma directive, which invokes undefined behavior according to the language definition. In practice, many C implementations recognize, for example, #pragma once as a rough equivalent of include guards — but GCC, upon finding a #pragma directive, would instead attempt to start Emacs running a simulation of the Towers of Hanoi. Failing that, it would try to launch other commonly distributed Unix games such as NetHack and Rogue.

Under some circumstances there can be specific restrictions on undefined behavior. For example, the instruction set architecture of a CPU might leave the behavior of some forms of an instruction undefined, but if the CPU supports memory protection then the architecture specification will probably include a blanket rule stating that no user-accessible instruction may cause a hole in the operating system's security; so an implementation of the architecture would be permitted to corrupt all user registers in response to such an instruction but would not be allowed to (say) switch into supervisor mode.

External links

ru:Неопределённое поведение uk:Невизначена поведінка