Clang and gcc produce different results when a pointer is reconstructed from integer arithmetic.
*A simple C test shows clang and gcc diverge when a pointer is reconstructed from integer math. The split reveals how language designers balance high‑level optimizations against low‑level expectations.*
A tiny C program is exposing a rift between two of the world’s most trusted compilers. By allocating two heap cells, converting their addresses to integers, and then fabricating a new pointer from the numeric difference, the author forces clang‑6.0 and gcc‑8 to reveal divergent assumptions about pointer validity. The test runs on a Mac, prints a constant difference of -96, and then stores through the reconstructed pointer. clang reports that the store overwrites the first allocation; gcc sometimes agrees, sometimes does not, depending on how the pointer is computed. The split is not a bug report but a window into how language specifications, compiler heuristics, and low‑level expectations collide.
The author allocates two 4‑byte heap cells with malloc, stores 3 in the first and 5 in the second, then casts the pointers to uintptr_t. The difference between the two integer representations is printed (‑96 on the author's Mac). A new pointer p is forged by adding that difference to the second integer value, and the program stores 7 through p. Finally it prints the addresses and contents of x, y and p. The code compiles with clang‑6.0 -O3 and gcc‑8 -O3 and produces observable differences in how the compilers treat the store through p.
Both compilers agree that the store through p changes *x* when p is computed as yi + diff. When the program is altered to compute p as yi - 96 directly, clang repeats the earlier behavior, but gcc now reports *x* unchanged at 3. The author explains that gcc regards the address derived from a guessed constant as illegitimate, while clang trusts the numeric equality. This split stems from each compiler's internal decision‑making about how far to trust integer‑derived pointers versus actual observations, a gray zone beyond the C standard.
The author notes that the program does not invoke undefined behavior: computing out‑of‑bounds pointers is UB, but manipulating integer‑typed values that originated from pointers is permitted. However, the standard offers only sparse guidance on the interaction between integers and pointers. It does not mandate that a store through a pointer reconstructed from an integer must affect the original object. The example therefore lives in a space where compiler writers balance optimization power against developer expectations without a clear normative rule.
The tension highlighted by the test is endemic to performance‑oriented languages such as C, C++, and Rust. They aim to appear high‑level enough for aggressive optimizations while still exposing low‑level pointer manipulation. The author lists possible mitigations: delegating low‑level work to another language as Java does with JNI, providing an unsafe mode as Rust does, or freely mixing high‑ and low‑level code as C and C++ already do. Each approach trades safety for control, and the pointer‑integer confusion illustrated by the test underscores why language designers must choose carefully.
No. Computing out‑of‑bounds pointers is UB, but using integer‑typed values derived from pointers is allowed, so the program itself does not invoke undefined behavior.
No. The standard gives only sparse guidance on integers derived from pointers and does not mandate the store through the reconstructed pointer to affect the original object.
C, C++, Rust (via unsafe code), and Java (via JNI) are cited as languages that allow mixing high‑level abstractions with low‑level pointer manipulation.
The experiment shows that even a well‑defined C program can yield opposite outcomes when compilers interpret integer‑derived pointers differently. As systems programmers push for both safety and performance, the gap highlighted here will force language designers and compiler teams to clarify the rules that currently sit in a legal gray area. Until the standards catch up, developers must assume that pointer arithmetic masquerading as integer math may not survive aggressive optimization.
Sources: [Hacker News article linking to https://blog.regehr.org/archives/1621]