Tech News
← Home  ·  All topics

Undefined Behavior

3 GoKawiil briefs on this topic

LWN discusses efforts to curb undefined behavior in C standard

An LWN.net article recounts a talk explaining why undefined behavior exists in C and how the language's standard, dating back to C89, was built around an abstract machine model to accommodate diverse historical hardware. The speaker outlined C's enduring strengths—portability, fast compilation, predictable binaries—while detailing how the standard only guarantees 'observable' behavior, leaving much else open to compiler implementers.

Compiler optimizations expose hidden UB risk in C/C++ type punning

A developer traced a hard-to-find bug to type punning via pointer casts, which behaves correctly at -O0 but breaks under -O2 optimization. The root cause is that pointer casts violate strict aliasing rules and constitute undefined behavior in C, even though they compile and run correctly on most platforms, and C++ treats type distinctions even more strictly than C.

C++26 standard makes empty infinite loops well-defined behavior

The upcoming C++26 standard, via proposal P2809R3, changes the language so that trivial infinite loops like 'while(true);' are no longer undefined behavior. Previously, compilers such as Clang could legally assume such loops terminate and optimize them away entirely, sometimes causing code after the loop to execute unexpectedly. Because the fix was also accepted as a defect report, compiler vendors may backport it to earlier C++ standard modes too.