HN
Today

What's the difference between an integer and a pointer? (2018)

A deep dive into the murky waters where C pointers and integers meet reveals a surprising truth: they're not interchangeable in the eyes of optimizing compilers. This article exposes how compiler interpretations of "pointer provenance" can lead to wildly different program behaviors. It's a fascinating look at the subtle complexities of low-level programming that continues to ignite heated debates about undefined behavior on HN.

64
Score
37
Comments
#5
Highest Rank
4h
on Front Page
First Seen
Nov 23, 2:00 PM
Last Seen
Nov 23, 5:00 PM
Rank Over Time
18568

The Lowdown

In the world of performance-oriented systems languages like C, C++, and Rust, there's a fundamental tension between enabling high-level optimizations and allowing low-level pointer manipulation. While assembly languages treat memory addresses as mere bitvectors and high-level languages firewall such abstractions, these systems languages try to do both, often leading to perplexing compiler behaviors.

This article illustrates this conflict with two C code examples:

  • Example 1: The author demonstrates converting two malloc'd pointers (x, y) to uintptr_t, calculating their diff, and then deriving a new pointer p from y and diff that happens to equal x. Both Clang and GCC correctly recognize that a write through p modifies x.
  • Example 2: By simply replacing the diff variable with its observed constant value (-96) in the derivation of p, Clang still behaves as before. However, GCC, operating at -O3, no longer considers p to alias x. This means a write to *p does not affect *x in GCC's optimized output, even though x and p point to the same address at runtime.

The author notes that while the C standard allows manipulation of integer-typed values derived from pointers, it provides sparse guidance on their round-trip conversion and behavior. This leads to scenarios where compilers like GCC and Clang make differing, yet arguably standards-compliant, decisions based on their internal reasoning about 'pointer provenance' – the metadata or origin associated with a pointer. The crucial takeaway is that optimizing compilers differentiate between pointers based on how they were derived, even if their underlying bit patterns are identical.

Ultimately, the article serves as a stark reminder that in languages capable of low-level memory access and aggressive optimization, the distinction between a pointer and a pointer-sized integer is far more complex than a simple bitvector equivalence, and this distinction can lead to surprising and undocumented compiler-dependent behavior.

The Gossip

Provenance Ponderings and Undefined Understandings

Discussion revolves around the concept of "pointer provenance" as the fundamental distinction between pointers and integers, with integers lacking this crucial metadata. There's a significant debate on whether the described compiler behaviors constitute Undefined Behavior (UB) or Implementation-Defined Behavior (IDB) in C, and the severe consequences of UB for compiler optimization and program reliability. Commenters emphasize that IDB, while potentially non-portable, is still a defined behavior, unlike UB which voids all guarantees.

GCC's Grasp on Guesses

Commenters analyze why GCC behaves differently than Clang in the second example, particularly regarding its interpretation of pointers derived from integer arithmetic. Some speculate it's an optimization that reorders operations, while others argue it's a consequence of GCC adhering strictly to the C standard's abstract machine model, which grants compilers significant freedom when faced with certain constructs. The core debate is whether this behavior is a "bug" or a deliberate, if surprising, outcome of aggressive optimization.

Pointers Aren't Just Plain Primitives

A prominent theme is the rebuttal of the common C programmer adage that "pointers are just integers." Commenters highlight that this assumption, while often practically true on common modern platforms, breaks down on segmented architectures (like 8086), Lisp machines, or when considering the C standard's lack of guarantees. They emphasize that `uintptr_t` is optional for a reason, and that the "same pattern of ones and zeroes" doesn't equate to the same semantic meaning or compiler treatment, especially with varying word sizes or non-linear address spaces.