Undefined Behavior
in C++
Discover why certain C++ code makes no guarantees about what will happen: it might crash, produce wrong results, or appear to work perfectly until it doesn't.
What Is Undefined Behavior?
When the C++ standard says a program has undefined behavior (UB), it means the standard imposes no requirements whatsoever on what the program does. The compiler, the operating system, and the hardware are all free to do absolutely anything.
This is not a bug in the compiler: it is a contract violation by the programmer. You wrote code the language explicitly does not define, so the implementation owes you nothing.
Why Does UB Exist?
The Nasal Demons Metaphor
Programmers jokingly say UB can make "nasal demons fly out of your nose." The point: once UB occurs, literally anything is a conforming result. There are no constraints on past, present, or future behavior.
The Behavior Spectrum
The C++ standard defines several categories of behavior. Understanding the differences is essential:
Implementation-Defined vs Undefined
| Implementation-Defined | Undefined Behavior | |
|---|---|---|
| Guaranteed? | Yes: consistent result per compiler | No: absolutely no guarantees |
| Documented? | Compiler must document its choice | No obligation to document anything |
| Portable? | Not across compilers, but predictable | Not portable, not predictable |
| Example | \(\text{sizeof}(\text{int})\) | Signed integer overflow |
| Can optimize away? | No: must honor its documented behavior | Yes: compiler may assume UB never happens |
The Core Rule of UB
This is sometimes called the "as-if" rule on steroids. Compilers don't just ignore UB: they exploit it for optimization. A branch that would only be reached via UB can be deleted. A variable that can only hold a certain value if UB happened can be assumed to hold a "safe" value.
Common Sources of UB
How to Detect and Avoid UB
Example 1: Signed Overflow
Example 2: Out-of-Bounds Access
Example 3: Use After Free
UB Can Time Travel
One of the most surprising properties of UB is that it can affect code before the undefined operation, not just after it. Because the compiler assumes UB never occurs, it can reason backwards.
Uninitialized Variables
Common Mistakes
UB Quick Reference
| Source of UB | The Danger | The Fix |
|---|---|---|
| Signed overflow | Compiler removes overflow checks | Use unsigned or check first |
| Null dereference | Crash, or null check removed | Check before dereferencing |
| Out-of-bounds | Corrupts memory, security holes | Use .at() or bounds check |
| Use after free | Reads garbage, security exploit | Smart pointers |
| Uninitialized var | Indeterminate value propagates | Always initialize |
| Data race | Torn reads, inconsistent state | Mutex / atomic |
Question 1 of 5
Which of the following is undefined behavior in C++?
Question 2 of 5
A programmer writes code that dereferences a pointer before checking if it is null. What might the compiler do?
Question 3 of 5
What is the key difference between implementation-defined behavior and undefined behavior?
Question 4 of 5
Consider this C++ code:
What is wrong with this code?
i <= 3 lets i reach 3, but valid indices for arr[3] are 0, 1, and 2. Accessing arr[3] is an out-of-bounds read: undefined behavior. The fix is to use i < 3.Question 5 of 5
Which compiler flag enables the Undefined Behavior Sanitizer to detect UB at runtime during testing?