hn.today

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

blog.regehr.org3 points0 comments
Screenshot of What's the difference between an integer and a pointer?

Systems languages such as C and C++ need to support both aggressive optimization and low-level manipulation of addresses. Compilers optimize by assuming pointers from separate allocations do not alias, yet programmers may inspect pointer representations, do arithmetic on them as integers, or construct pointers from integer values. These goals can conflict. A demonstration converts two allocated pointers to integers, combines their values to fabricate a pointer equal to the first, then stores through it. GCC and Clang report that the first allocation changed, while the second did not. The program is not undefined merely because it performs arithmetic on integers derived from pointers, but the C standard provides little guidance on what such conversions and resulting accesses must mean.

The proposed research addresses this uncertainty with a memory model that gives pointer-integer conversions defined semantics while retaining compiler optimizations. One concern is that treating a conversion as potentially undefined could make an otherwise valid optimization introduce undefined behavior by moving the conversion into a loop that might never run; but restricting when conversions can move may also inhibit optimization. A formal model can clarify which transformations preserve program behavior and help validate optimizers. Discussion raises alternatives, including treating conversions as opaque operations or adding pointer-like types with explicit access lifetimes. The researchers argue that a precise model is practical rather than inherently heavyweight, and say it should work with architectures supported by LLVM, though bit-addressable systems may need extension.

Read on blog.regehr.org0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.