Programming Expert Argues All Non-Trivial C/C++ Code Contains Undefined Behavior

Original: Everything in C is undefined behavior

Why This Matters

Highlights ongoing memory safety challenges driving industry shift toward safer languages

A 30-year C/C++ programmer argues that undefined behavior is pervasive in C and C++ code, making it impossible to write completely correct programs. The post highlights subtle UB cases like misaligned memory access that vary across architectures.

Thomas Habets, a longtime C/C++ programmer, published a blog post arguing that all non-trivial C and C++ code contains undefined behavior (UB). He references Cardinal Richelieu's famous quote, adapting it to programming: finding UB in any six lines of expert C code. Habets explains that UB doesn't just occur with optimizations - it means compilers can assume code is valid, leading to unpredictable behavior across compiler stages. He provides examples like misaligned pointer access, which behaves differently across architectures: Linux Alpha may trap to kernel for software emulation, SPARC causes SIGBUS crashes, while x86 is typically forgiving. The post argues that since no one can write UB-free C/C++ code consistently, the languages are unsuitable for today's development environment compared to their origins in 1972-1985.

Source

blog.habets.se — Read original →