Reducing undefined behavior in the C language
6 hours ago
- C remains a great language for its portability, stability, fast compilation, and performance.
- Undefined behavior (UB) stems from historical hardware diversity and gives compilers great freedom, leading to unpredictable optimizations and disagreements.
- C23 and C2y standards have removed many UB instances; C23 removed old-style definitions, sign-magnitude support, and trigraphs, while adding bit-precise types and checked integer operations.
- Tools such as compiler warnings, static analyzers, sanitizers, and formal verification help detect and mitigate UB.
- Memory safety in C breaks into type safety, spatial safety, and temporal safety; improvements are possible through annotations, tools, and architectures like CHERI.
- Full memory safety may require expensive run-time checks or formal verification; a combination of restricted language and formal verification offers near-term promise.