Undefined behavior
Undefined behavior is program execution or an operation for which the applicable specification imposes no requirements on the resulting behavior.
100 C++ Mistakes and How to Avoid Them
Undefined behavior is a program operation whose result or behavior is not defined by the applicable language standard.
- How it is used
- Frames undefined behavior as a correctness risk that can make apparently working C++ code unpredictable or unsafe.
- How deep it goes
- Defines undefined behavior and works through causes, consequences, and corrections in several C++ scenarios.
- Examples
- Uses an uninitialized age member, deletion through a base pointer, and a returned reference to a local string.
- What you can do after
- Diagnose/debug common undefined-behavior risks by checking member initialization, polymorphic deletion, and references returned to local objects.
Read how 100 C++ Mistakes and How to Avoid Them explains undefined behavior →
Modern C
Undefined behavior is a C execution condition for which the language standard imposes no requirements on the program's behavior.
- How it is used
- Uses undefined behavior to explain why apparently working C code can fail under different compilation conditions.
- How deep it goes
- Explains undefined states and compiler assumptions through signed-loop optimization, then identifies hazards in library calls, pointers, aliasing, and threads.
- Examples
- Contrasts signed and unsigned incrementing loops and examines mismatched printf arguments, null pointers, incompatible pointer aliases, and shared thread state.
- What you can do after
- Evaluate C operations for undefined behavior and recognize why observed results cannot justify reliance on them.
Modern C, Third Edition
Undefined behavior is a C execution condition for which the standard imposes no requirements on the program's subsequent behavior.
- How it is used
- Frames undefined behavior as program failure and directs attention to its causes rather than only its possible manifestations.
- How deep it goes
- Explains undefined states and compiler assumptions through arithmetic and formatted output, while deferring a fuller discussion of program failure.
- Examples
- Uses signed and unsigned counter loops and mismatched printf format specifiers to illustrate undefined behavior.
- What you can do after
- Evaluate/understand why apparently successful execution does not establish correctness and why compiler assumptions matter.
Read how Modern C, Third Edition explains undefined behavior →
C++ Concurrency in Action, Second Edition
Undefined behavior is program behavior for which the C++ language standard imposes no requirements.
- How it is used
- Identifies program behavior the C++ standard leaves unconstrained, including data races and invalid parallel-algorithm calls.
- How deep it goes
- Defines the consequence and explains data-race and parallel-policy cases, without surveying undefined behavior exhaustively.
- Examples
- Uses a detached thread's local buffer and a lock-free stack pop on an empty stack as examples.
- What you can do after
- Diagnose data-race and parallel-policy cases, and identify atomic or mutex synchronization as ways to define shared access.
Read how C++ Concurrency in Action, Second Edition explains undefined behavior →
Refactoring to Rust
Undefined behavior is program execution whose effects are not constrained by the language or platform specification.
- How it is used
- Constrains raw-pointer handling at a Rust/C FFI boundary by requiring validation before dereference.
- How deep it goes
- Defines null-pointer dereference as undefined behavior and demonstrates a null check before an unsafe write, without covering broader cases.
- Examples
- Uses a C calculator's solution out-parameter and Rust's solve function, first producing an uninitialized result and then writing 1024.
- What you can do after
- Apply it via null checks and narrowly scoped unsafe blocks when writing through C-provided pointers.
Learn C++ by Example
Undefined behavior is program execution for which the C++ language imposes no requirements on the result.
- How it is used
- Uses integer-overflow experiments to reveal language limits and motivate validation with algorithms and assertions.
- How deep it goes
- Defines undefined behavior and demonstrates overflow detection, but stops short of cataloguing its causes or explaining compiler internals.
- Examples
- Uses positive-number row generation, overflowing int values, Visual Studio wraparound, and non-negative checks as practical examples.
- What you can do after
- Diagnose/debug with it by testing numeric assumptions and recognizing that observed overflow behavior is not portable.
Learn Rust in a Month of Lunches
Undefined behavior is program execution whose effects are not constrained by the language specification because a required safety invariant has been violated.
- How it is used
- Explains why unsafe Rust requires explicit responsibility for operations that can violate aliasing, race, or validity assumptions.
- How deep it goes
- Demonstrates UB through races and invalid unchecked unwrapping, but does not develop a formal language-level taxonomy.
- Examples
- Uses concurrent increments of static mut and invalid Option::unwrap_unchecked() calls.
- What you can do after
- Diagnose unsafe operations by reading compiler warnings and recognizing races or violated unchecked assumptions.
Read how Learn Rust in a Month of Lunches explains undefined behavior →
Also covered in
- Learn Haskell by Example
- Hacking Cryptography
- Memory Safe C++In early access (MEAP)
- Learn Go with Pocket-Sized Projects
- Writing for Developers: Blogs that get read
- Systems Programming with ZigIn early access (MEAP)
What to know first
- C++C++ is a statically typed, general-purpose programming language that supports procedural, object-oriented, generic, and low-level systems programming.
- garbage collectionGarbage collection is an automated process that identifies and reclaims or removes memory, data, or other items no longer reachable or needed by a system.
- unsigned integerAn unsigned integer is an integer type or representation whose values are nonnegative.
- precisionNumerical precision is the fineness with which a numerical representation distinguishes values, often determined by the number of significant digits or bits available.
Related concepts
- data raceproduced by · A data race is a concurrent access to shared data in which at least one access is a write and the accesses are not adequately synchronized.
- numerical overflowproduced by · Numerical overflow occurs when an arithmetic result falls outside the range representable by its numeric type or format.
- double delete errorproduced by · A double-delete error is a memory-management error in which the same allocated memory is deallocated more than once.
- format stringproduced by · A format string is a character sequence containing literal text and directives or placeholders that govern how supplied values are represented in formatted text.
Questions about undefined behavior
What is undefined behavior?
Undefined behavior is program execution or an operation for which the applicable specification imposes no requirements on the resulting behavior.
Which Manning books explain undefined behavior?
100 C++ Mistakes and How to Avoid Them, Modern C, Modern C, Third Edition, C++ Concurrency in Action, Second Edition, Refactoring to Rust, Learn C++ by Example and Learn Rust in a Month of Lunches.
What should I know before learning undefined behavior?
Manning books that teach undefined behavior usually introduce C++, garbage collection, unsigned integer and precision first.
What is undefined behavior also called?
Undefined behavior is also called ub.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.






