Hacker News new | ask | show | jobs
by spwa4 10 days ago
There is no difference.

Yes, I'm a C programmer, how did you know?

7 comments

It doesn't sound like you are or you would know C doesn't define either precisely enough to make that statement true.
On the contrary, the vast majority of C programmers don't know C well enough to understand the difference between integers and pointers.

Using a tool doesn't mean understanding that tool, just means you know how to follow some repetitive processes over and over again.

They are definitely not integers on the Symbolics Lisp machine. The C standard allows for atypical representations. [u]intptr_t is optional for this reason.
Sure, but if you're not an embedded programmer, if you're not working with odd architectures, then the difference between pointers and unsigned integers of the same size in C on common current platforms' C ABIs, is minimal. Of course there are footguns here: you can't conjure up and dereference a pointer from an integer that isn't itself obtained from casting from a valid pointer.
C doesn't provide any guarantee that a pointer can be stored in an integer.

The language only guarantees that an integer is at least 16 bits. There is no guarantee that a compiler that supports a > 16 bit address space will chose to make integers large enough to hold a pointer, and for example gcc still uses 32-bit integers for 64-bit processors.

I'm not sure if C even requires a pointer to be just a linear address, as opposed to something structured.

> I'm not sure if C even requires a pointer to be just a linear address, as opposed to something structured.

It doesn't. The standard does not require any particular representation for pointers. It does specify that addition and subtraction on them is possible - but does not specify how this occurs - only that for example, if you have an array `int x[]`, then `x + 1` should point to the second item (index 1) in the array.

WG14 (the standards committee for the C programming language) does not agree with you.

https://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_260.htm

There is a counter example in the OP.
There's the voice of a C programmer who hasn't used hardware with a different word size to address space...
But (almost) every hardware has some integer type that has the same size as what is needed to represent the address space, because (almost) every hardware has some way of manipulating addresses at the assembly level, and (almost) every C implementation has some integer type that generates those assembly instructions.

I say "almost" because I am not absolutely certain that this is true of absolutely every architecture. Feel free to point out one that breaks these rules, if there is one.

In principle a pointer can be a tuple of two other things. Think of CHERI, segmented architectures, etc. In practice on current, modern, general purpose, non-embedded platforms' C ABIs pointers are equivalent to unsigned integers of the same size.
Arguably they're more like signed integers in the case of amd64 - due to address canonicalization. Eg, with 4-level paging we have a 48-bit address space, and valid pointers have the signed integer values `-2^48..+2^48-1`.

They can be further complicated by UAI/LAM, which relaxes the canonicalization constraints and allows bits 48..62 (the high bits, excluding the sign bit) to be anything - so we can have pointers that compare unequal but point to the same address - a potential source of exploits (There's already been Spectre-like vulnerabilities shown for LAM). Fortunately nobody really uses LAM - it needs enabling by the kernel to be used in user space to begin with.

You don't need to interpret the high bit as a sign bit. uintptr_t (unsigned) shows we can interpret pointers as unsigned integers when we can interpret them as integers at all.
You can interpret it as uintptr_t - particularly now that the latest C standards require two's complement representation of integers - but pointers on amd64 are more like signed ints.

The sign bit must match the highest bit of the virtual address (47th bit counting from 0 in the case of 4LP, or the 56th bit for 5LP). (And without LAM/UAI, all the bits in between must match too.)

Eg, with 4LP, if you cast a pointer to `uintptr_t`, shift it left 16 bits, then shift right 16 bits - then an address in the higher half (usually in the Kernel) would be non-canonical. Do the same for `intptr_t` it will be canonical because the shift right is an arithmetic shift.

On other architectures this isn't the case. I know AARCH64 for example does not use canonicalization. If you were doing the same shift left/right by 16 as above on AARCH64, you'd want a uintptr_t.

Segmented 8086 is always the fun one.
I still have occasional nightmares about that. Once I got a 32-bit C/C++ compiler for DOS, I never looked back.
I agree, I have passed void* to the JVM by casting to int more often than I can count
They're not expressing an opinion, they're expressing a mistaken understanding of a factual point. The C language is defined by a standard, not by what happens to work on the platform you're currently working with.
You should read TFA.