RC RANDOM CHAOS

Three decades in: nobody actually writes correct C or C++

· via Hacker News

Original source

Everything in C is undefined behavior

Hacker News →

A veteran C/C++ developer argues that undefined behavior is so pervasive in the languages that even disciplined experts cannot reliably avoid it. The core misconception he targets is the idea that UB only bites when optimizations are enabled. UB is not the compiler punishing sloppy code; it is the compiler being told certain situations cannot occur, so neither the compiler nor the hardware needs a coherent story for what happens when they do. The intent a human reads off the page often has no way to survive the trip through compiler stages.

The piece walks through cases that look innocuous but are formally undefined: dereferencing a misaligned int pointer (fine on x86, SIGBUS on SPARC, kernel-emulated on Alpha), and even just casting a byte buffer to int* before any load. Atomic operations on misaligned or page-spanning objects fall in the same trap. Passing a plain signed char to isxdigit can index past a lookup table because values outside 0-127 become negative ints. Converting a float to int safely against INT_MAX requires a half-dozen guards for non-finite values, rounding during the reverse cast, and signed overflow on the follow-up arithmetic — all to replace a one-line multiply-and-cast that almost everyone actually writes.

The broader argument is that if every nontrivial codebase contains UB and no programmer can be expected to catch all of it, blaming individual developers is unfair. The 1972 and 1985 assumptions behind C and C++ no longer match the architectures, toolchains, or safety expectations of 2026, and the industry should stop pretending otherwise.

Read the full article

Continue reading at Hacker News →

This is an AI-generated summary. Read the original for the full story.