Undefined Behavior / UB
Updated 2026-07-28
Summary
In computer programming, undefined behavior refers to the result of executing certain computer code whose behavior, in the current program state, is not specified by the language standard. It commonly occurs when a translator (compiler) makes certain assumptions about the source code, and these assumptions do not hold at runtime. Some programming languages have undefined behavior in certain situations, most notably C and C++.
Definition
In computer programming, undefined behavior refers to the result of executing certain computer code whose behavior, in the current program state, is not specified by the language standard. It commonly occurs when a translator (compiler) makes certain assumptions about the source code, and these assumptions do not hold at runtime. Some programming languages have undefined behavior in certain situations, most notably C and C++. In the standards of these languages, the semantics of certain operations are defined as undefined, typical examples being program errors such as out-of-bounds array access. The standard allows a specific implementation of the language to assume that any conforming program will not exhibit such behavior. Specifically in C/C++, the compiler may optionally issue a diagnostic, but it is not required to: for undefined behavior, any reaction by the language implementation is considered correct, similar to don't-care terms in digital logic. Although compiler implementations may issue diagnostics for undefined behavior, it is the programmer's responsibility to ensure that the code they write does not trigger undefined behavior.
Introduction
How to use
In computer programming, undefined behavior refers to the result of executing certain computer code whose behavior, in the current program state, is not specified by the language standard. It commonly occurs when a translator (compiler) makes certain assumptions about the source code, and these assumptions do not hold at runtime. Some programming languages have undefined behavior in certain situations, most notably C and C++.
Mechanism
In programming languages, operations whose results are not specified by the standard (such as null pointer dereference, signed overflow) are called undefined behavior (UB). The compiler may assume that they do not occur and perform aggressive optimizations based on that. The mechanism is that the standard leaves gaps to allow for optimization.
Practice
In progress 0/3Practice this model in one real situation:
Cases
- UB in C/C++ leads to weird cross-platform bugs; optimized code behavior changes.
Common pitfalls
- Thinking 'it runs on my machine' is safe; UB has no 'correct' result.
Sources
Source support: Explicit
References currently contain only Wikipedia links; primary sources still need review.
- zh.wikipedia.orghttps://zh.wikipedia.org/wiki/%E6%9C%AA%E5%AE%9A%E4%B9%89%E8%A1%8C%E4%B8%BAverified
account_treeGenealogyexpand_more
PRIVATE NOTES · Only visible to you
SAVED Q&A
ENTRY Q&A · Private saving available
Ask with a clear boundary
thinkingmodels answers from published entry context only.
Your question is sent to thinkingmodels. The answer uses public entry context only.
RELATED MODELS