Undefined Behavior

In computer programming, undefined behavior refers to computer code whose behavior is unpredictable. It 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, meaning that the programmer can't predict what will happen.

For example, in C the use of any automatic variable before it has been initialized yields undefined behavior, as would division by zero or indexing an array outside of its defined bounds (see buffer overflow). This specifically frees the compiler to do whatever is easiest or most efficient, should such a program be submitted. In general, any behavior afterwards is also undefined. 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. When an instance of undefined behavior occurs, so far as the language specification is concerned anything could happen, maybe nothing at all.

Under some circumstances there can be specific restrictions on undefined behavior. For example, the instruction set specifications of a CPU might leave the behavior of some forms of an instruction undefined, but if the CPU supports memory protection then the 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 actual CPU would be permitted to corrupt any or all user registers in response to such an instruction but would not be allowed to, for example, switch into supervisor mode.

In C and C++, implementation-defined behavior is also defined which requires the implementation to document what it does, thus more restrictive than undefined behavior.

Read more about Undefined Behavior:  Examples in C and C++, Risks of Undefined Behavior, Compiler Easter Eggs

Famous quotes containing the words undefined and/or behavior:

    That undefined and mingled hum,
    Voice of the desert never dumb!
    James Hogg (1770–1835)

    If you are willing to inconvenience yourself in the name of discipline, the battle is half over. Leave Grandma’s early if the children are acting impossible. Depart the ballpark in the sixth inning if you’ve warned the kids and their behavior is still poor. If we do something like this once, our kids will remember it for a long time.
    Fred G. Gosman (20th century)