Skip to content

Undefined Behavior

A programming-language case for which the governing specification imposes no requirements on implementation behavior.

Version
v2 · 2026-11-14 · History
Domain-specific #
13778
Domain group
Applied Sciences & Engineering
Origin domain
Computer Science & Software Engineering
Subdomains
Programming Languages, Language Standards → Computer Science & Software Engineering
Aliases
UB, Undefined program behavior

Core Idea

Undefined behavior is a case for which a programming-language specification imposes no requirements on an implementation. Under the cited C++ working draft, an execution that triggers such a condition has no standard-mandated result for that case. A compiler can optimize defined executions on the assumption that those conditions are avoided.[1][2]

How would you explain it like I'm…

Rulebook Says Nothing

A computer language has a big rulebook that says what each instruction should do. For a few things, the rulebook says nothing at all — not "it breaks," not "it stops," nothing. So whoever builds the computer helper can let anything happen, and you can't count on any one result.

When the Rulebook Says Nothing

People who design programming languages write a rulebook called a standard. For most actions it says exactly what must happen. But for some actions, like a math result too big to fit in a number's box, the rulebook makes no promises at all: no wrap-around, no error message, no crash, nothing in particular. That is undefined behavior. The program-translating tool, the compiler, is allowed to assume your program never does those things, so the result can surprise you.

Undefined Behavior in Language Standards

In a programming-language standard, undefined behavior means the standard places no requirements on what a conforming implementation does. It is not 'it crashes' or 'it gives a random number'; it is the absence of any promise. In the C++ draft standard, for example, signed integer arithmetic whose result doesn't fit its type is undefined, and so is shifting by a negative amount or by at least the width of the value. Because compilers only have to get defined programs right, they can optimize as if those situations never happen, which can wreck what a programmer hoped would occur. Whether something is undefined depends on the specific language and version.

 

Undefined behavior (UB) is a contract category in a language specification: for an operation classified as UB, the governing document imposes no requirements whatsoever on a conforming implementation. Reaching such an operation does not entitle the programmer to a wraparound result, an exception, a diagnostic, a crash, or any other specific outcome. Compilers exploit this by reasoning only about executions in which the prohibited condition never arises; optimizations valid for all defined executions need not preserve whatever the programmer expected in the undefined case. In the cited C++ working draft, signed arithmetic overflowing its type's representable range is UB, as are shifts by a negative count or by a count at least the width of the promoted left operand. The classification is tied to a particular standard and version; other languages and intermediate representations can treat the same operations differently. A processor manual's 'UNPREDICTABLE' label is a distinct notion and should not be merged into the language-standard category.

Scope of Application

The exact boundary depends on the language and version. The C++ draft classifies an unrepresentable signed arithmetic result and a shift count outside the allowed range as UB. It separately defines unspecified, implementation-defined and erroneous behavior. Processor “UNPREDICTABLE” language and LLVM IR poison have their own rules, not automatic aliases.[3][4][5]

Clarity

“Undefined” is not merely a result the programmer does not know. Unspecified behavior is bounded by allowed outcomes; implementation-defined behavior must be documented; recent C++ erroneous behavior is well-defined but recommended to diagnose. UB is the no-requirements category.[1][6][5]

Manages Complexity

The category marks cases a standard does not constrain, allowing compiler assumptions for defined executions. That shifts prevention onto program authors and tools; a sanitizer can detect selected reached cases but does not itself define the language rule.[2][7]

Abstract Reasoning

Name the standard version and clause, determine whether a specific execution reaches its undefined condition, and avoid inferring portable behavior from one observed run. The latest C++ draft has more precise defined-prefix wording than the blanket slogan that absolutely nothing before a UB event can be constrained.[6]

Knowledge Transfer

The contract question transfers across languages and intermediate representations, but the classifications and consequences must be rechecked per specification. Erroneous Program is a separate identity-review hold, not a UB alias; the generic missing-guarantee skeleton does not by itself make this a prime.

Neighborhood in Abstraction Space

Undefined Behavior sits in a sparse region of the domain-specific corpus (84th percentile for distinctiveness): few abstractions share its structure, so a faithful description tends to retrieve it precisely.

Family — Program Scope & Nesting Disciplines (10 abstractions)

Nearest neighbors

Computed from structural-signature embeddings · 2026-11-14

References

[1] C++ standards draft editors, “C++ Working Draft Eelis Snapshot c7015b485cc3db8efaa9dfb9ff0809c5394a4ed1”, unofficial eel.is HTML rendering of “Working Draft: Programming Languages — C++” generated 2026-08-23, [defns.undefined] definition; not an ISO publication. registry ↩a ↩b

[2] LLVM compiler author's UB explanation. registry ↩a ↩b

[3] C++ standards draft editors, “C++ Working Draft Eelis Snapshot c7015b485cc3db8efaa9dfb9ff0809c5394a4ed1”, unofficial eel.is HTML rendering of “Working Draft: Programming Languages — C++” generated 2026-08-23, [expr.pre] arithmetic rule; not an ISO publication. registry ↩

[4] C++ standards draft editors, “C++ Working Draft Eelis Snapshot c7015b485cc3db8efaa9dfb9ff0809c5394a4ed1”, unofficial eel.is HTML rendering of “Working Draft: Programming Languages — C++” generated 2026-08-23, [expr.shift] shift-count rule; not an ISO publication. registry ↩

[5] C++ standards draft editors, “C++ Working Draft Eelis Snapshot c7015b485cc3db8efaa9dfb9ff0809c5394a4ed1”, unofficial eel.is HTML rendering of “Working Draft: Programming Languages — C++” generated 2026-08-23, [defns.erroneous] definition; not an ISO publication. registry ↩a ↩b

[6] C++ standards draft editors, “C++ Working Draft Eelis Snapshot c7015b485cc3db8efaa9dfb9ff0809c5394a4ed1”, unofficial eel.is HTML rendering of “Working Draft: Programming Languages — C++” generated 2026-08-23, [intro.abstract] distinctions and defined-prefix wording; not an ISO publication. registry ↩a ↩b

[7] Clang UndefinedBehaviorSanitizer documentation. registry ↩