Cognitive Scaffold

Preparing your thinking workspace

arrow_back_ios_new
MENTAL MODEL · M1048

Undefined Behavior / UB

Undefined Behavior / UB
BehaviorHigh supportPsychology
Included
account_tree

Updated 2026-07-28

Loading revision record…

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.

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/3

Practice this model in one real situation:

Check to track your progress (stored locally)
Learning progress0%

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.

  • link
    zh.wikipedia.orghttps://zh.wikipedia.org/wiki/%E6%9C%AA%E5%AE%9A%E4%B9%89%E8%A1%8C%E4%B8%BAZH · Explicit
    verified
account_treeGenealogyexpand_more

RELATED MODELS