The unpredictable outcome of compiling or executing a program which breaks rules of the language neither compiler, interpreter nor runtime-system have to enforce. DO NOT USE this tag for questions regarding the data type or return value of "undefined". In those cases, the [undefined] tag should be used instead.
In computer programming, undefined behavior (informally "UB") refers to computer code whose behavior is not specified by the programming language standard under certain conditions.
The standards for some languages, most notably C and C++, leave certain aspects undefined, meaning the standard imposes no requirements whatsoever on the outcome. Implementations may regard such actions as erroneous, diagnosing them or not as they see fit, or may specify that they behave in some possibly-useful fashion without regard for whether the Standard requires them to do so.
For example, accessing beyond the last element of an array in C might be diagnosed by the compiler if the array index is known during compilation, or might return a garbage value from uninitialized memory, or return an apparently sensible value, or cause the program to crash by accessing memory outside the process' data address space.
Undefined Behavior most often causes confusion and controversy in situations where some parts of the Standard, an implementation's documentation, an earlier standard, or K&R's The C Programming Language describe the behavior of some action, but another part of the Standard characterizes the action as Undefined Behavior. In cases where the described behavior happens to be useful for the task at hand, the cost of behaving as described would often be less than the cost to the programmer of having to find some other way to accomplish the same task. In cases where it does not benefit the task at hand, however, upholding the behavior may add cost with no offsetting benefit. Because implementations are used for a variety of tasks with differing requirements, and because compiler writers should be better placed than the committee to know their various customers' needs, the authors of the Standard treat the question of whether to behave as described in such cases as a "quality of implementation" issue outside their jurisdiction.
Undefined behavior is commonly shortened to "UB". This informal abbreviation appears frequently in discussions regarding undefined behavior.
Formal definition of undefined behavior, as stated by the standards:
C11 3.4.3
undefined behavior
behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements
C++11 1.3.24
undefined behavior
behavior for which this International Standard imposes no requirements[ Note: Undefined behavior may be expected when this International Standard omits any explicit definition of behavior or when a program uses an erroneous construct or erroneous data. Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed. —end note ]
Tag usage
Use this tag for:
- Questions asking if a certain code contains undefined behavior.
- Questions regarding the various forms of documented undefined behavior in the given programming language.
Avoid using this tag for:
- Questions asking why undefined behavior gave a certain result. It is often impossible to provide a meaningful answer to such questions.
Always use this tag together with the relevant programming language tag.
Useful C and C++ questions / canonical duplicates
How to explain undefined behavior to know-it-all newbies?
Undefined, unspecified and implementation-defined behavior.
Undefined behavior and sequence points.
Why are these constructs (using ++) undefined behavior?
External links
Wikipedia: Undefined behaviour
A Guide to Undefined Behavior in C and C++, Part 1
What Every C Programmer Should Know About Undefined Behavior, part 2, part 3
Isn't a Standard's whole job to standardize these things?
Implementation-defined, unspecified, and undefined behavior. What do these mean?
I just tried it on an ANSI-conforming compiler, and got the results I expected