What is undefined behavior? Why does the C++ standard allow it?
Last updated
Undefined behavior is any construct with no defined semantics. A program triggering it is invalid and the standard guarantees nothing.
The standard allows it for performance and optimization freedom: no mandatory bounds, overflow, or null checks, and compilers can assume UB never occurs. It also lets each platform use its native behavior.
The trade-off is that correctness becomes the programmer's burden ("don't pay for what you don't use" at the cost of safety).