I had a bug that took me a while to track down. The problem was type punning. A pointer cast worked fine at -O0 and silently broke at -O2 . The C vs C++ distinction here is genuinely treacherous, and most blog posts on the topic get it wrong. Type punning is interpreting memory as different types between reads and writes. It’s essential for serialisation, network protocols, and low-level hardware access. The problem is that “works in practice” and “has defined behaviour” are different things. The Spectrum from Safe to UB In C, the safe ways to type pun are union and memcpy . Pointer casts are technically undefined behavior under strict aliasing rules, even though they work on every compiler you’ll encounter. Unions A union lets you write as one type and read as another. This is defined behavior in C: union { float f ; uint32_t bits ; } pun ; pun . f = 3 . 14 f ; uint32_t exp = ( pun .…