Undefined behavior
Undefined behavior is program execution for which the applicable language or system specification imposes no requirements on the resulting behavior or effects.
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 mistake requiring cautious avoidance in production and legacy C++ code.
- How deep it goes
- Defines undefined behavior and explains several object-lifetime causes, but stops short of a complete language-standard taxonomy.
- Examples
- Uses uninitialized Person members, base-pointer deletion of Square, and returning a local concatenated string.
- What you can do after
- Diagnose/debug with it by recognizing unsafe reads, polymorphic deletion, and dangling returns, then apply initialization, virtual destructors, or value returns.
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 seemingly working C programs can fail or be optimized unexpectedly.
- How deep it goes
- Explains consequences through signed overflow and gives operational rules for pointers, formatting, aliasing, and thread access without fully developing concurrency ordering.
- Examples
- Contrasts signed and unsigned incrementing loops and identifies invalid pointer dereferences, mismatched formatted output, and conflicting thread accesses.
- What you can do after
- Evaluate/understand it by checking whether signed arithmetic, pointer use, formatted output, aliasing, or shared access can make execution undefined.
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
- Identifies operations whose execution invalidates assumptions about a C program’s continuation.
- How deep it goes
- Explains compiler assumptions and unreliable outcomes through signed arithmetic, while deferring a fuller treatment of program failure.
- Examples
- Compares signed and unsigned increment loops and examines mismatched printf format specifiers.
- What you can do after
- Diagnose/debug with it by identifying invalid operations rather than trusting apparently normal output.
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 →
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 is the condition in which an arithmetic result falls outside the range representable by its numeric type or format.
- signed arithmetic overflowproduced by · Signed arithmetic overflow is the condition in which an operation on a signed integer produces a result outside the range representable by its type.
- virtual destructoraddressed by · A virtual destructor is a destructor whose dynamic dispatch ensures that destroying an object through a base-class interface invokes the derived-class destructor.
- format stringproduced by · A format string is a character sequence containing literal text and directives that specify how supplied values are represented in formatted input or output.
- double delete errorproduced by · A double-delete error is a memory-management error in which the same allocated memory is deallocated more than once.
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.






