What is undefined behavior? Why does the C++ standard allow it?

Last updated 21 June 2026

  • 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).

  • https://www.geeksforgeeks.org/cpp/undefined-behavior-c-cpp/
All C++ interview questions