HN Hall of Fame Weekly email

A Guide to Undefined Behavior in C and C++

blog.regehr.org Essays & writing Essays & articles Software engineering Class of 2023-08 Hall of Fame
Screenshot of blog.regehr.org captured 2026-07-20
Page preview · captured 2026-07-20

Resurfaced independently across 9 calendar years, with breakout response in 3 of them.

submissions
15
submitters
15
observed span
2010–2025
peak thread · 185 comments
111 pts
latest 20+ return · 2025-03-13
79 pts

Submission timeline

2007–2026

One slot for every year since HN launched. Height is that year's peak points; orange marks a 100+ point or 50+ comment breakout. Select a bar to open its strongest thread.

First comments on top threads

HN comment order

If you want some nice examples of how undefined behavior results in weirdness, see https://devblogs.microsoft.com/oldnewthing/20140627-00/?p=63... An interesting example from there is how the compiler can turn int table[4]; bool exists_in_table(int v) { for (int i = 0; i <= 4; i++) { if (table[i] == v) return true; } return false; } Into: bool exists_in_table(int v) { return true; }

This is an area the newer languages get right - I don’t think Rust or Go has any undefined behavior? I wish they would have some kind of super strict mode for C or C++ where compilation fails unless you somehow fix things at the call sites to tell the compiler the behavior you want explicitly.

For those viewing this thread, one year after this article was written, this was standardized: https://en.cppreference.com/w/cpp/numeric/fenv

> undefined behavior in C/C++ is that it simplifies the compiler’s job, making it possible to generate very efficient code in certain situations. I've been growing more skeptical of this claim over time. First, more evidence would be nice instead of "some loops". What is a program (not a benchmark) that has an observable performance difference when compiled with and without -fwrap? Second, it seems counter productive. The word is now out that if you use signed ints, the compiler…

The first top-level comment from each of the four biggest threads, in HN’s own order. Excerpts are shortened; open a comment for full context.

Breakout years
3

100+ points or 50+ comments

Total points
373

reference only — not used in Hall rules or ranking

Total comments
365

reference only — not used in Hall rules or ranking

Every submission