Advertisement
Intermediate C++, Undefined Behavior

What are common causes of undefined behavior?

Short answer

Common UB: out-of-bounds array/container access, dereferencing null or dangling pointers, use-after-free/double-free, reading uninitialized variables, signed integer overflow, data races (unsynchronized concurrent access), violating strict aliasing, invalid downcasts with…

Save C++ mock interview
Advertisement
AModel answer
Common UB: out-of-bounds array/container access, dereferencing null or dangling pointers, use-after-free/double-free, reading uninitialized variables, signed integer overflow, data races (unsynchronized concurrent access), violating strict aliasing, invalid downcasts with static_cast, and returning references to locals. Many are memory- or lifetime-related.
int x; std::cout << x;   // reading uninitialized value: UB
Real-world example
A data race between two threads on the same variable is UB and produces intermittent, hard-to-reproduce bugs.
Likely follow-ups
  • Why is a data race UB? | Name several sources of UB.
Related topics