logoalt Hacker News

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

70 points • by germanrabbit • last Saturday at 8:49 AM • 39 comments • view on HN

Comments

andai • today at 4:27 PM

Article says pointers and its are both just bitvectors, but doesn't explain what a bitvector is.

It seems to be a vector of bits? Except in practice, a bitvector is just an int where you're using the binary digits to store stuff? And you gotta use these awkward bitwise operators to actually use it? (Popular in 90s video games, to save RAM!)

Or was it another way of saying "ultimately it's the same pattern of ones and zeroes so you can do whatever you want with it as long as you know what you're doing" ?

➕ show 1 reply
notorandit • today at 5:39 PM

Assembly programmer here.

Depending upon the architecture, not all values can be memory addresses (aka pointers) due to alignment constraints. Then other way around is possible as long as memory addresses have at least l the same number of bits as integers.

Finally, as long as you don't dereference an integer used (casted) as a pointer, there is no difference. Like equality comparison.

kibwen • today at 5:22 PM

Recommended further reading in this blog series by Ralf Jung: https://www.ralfj.de/blog/2020/12/14/provenance.html

hn_submit • today at 3:06 PM

Context.

bryanlarsen • today at 3:14 PM

Pointers have provenance, integers don't.

➕ show 1 reply
glitchc • today at 3:23 PM

I don't think GCC is doing something special, rather that -O3 might be optimizing out the subtraction. Try with -O0, I suspect you will get the same result as clang.

➕ show 2 replies
pjmlp • today at 2:47 PM

2018, and some of the comments are already outdated, SPARK 2014 is out there with ownership model.

jojogeo • today at 4:40 PM

One is a mathematical construct, the other is a dog?

stronglikedan • today at 3:23 PM

well, their names, for starters...

spwa4 • today at 3:18 PM

There is no difference.

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

➕ show 4 replies
m00dy • today at 3:54 PM

range of values

cowlevel • today at 3:36 PM

> First, does this C program execute undefined behavior? It does not! Computing out-of-bounds pointers is UB but we are free to do whatever we like with integer-typed values that were derived from pointers. Second, does the C standard mandate the behavior that we saw here? It does not!

Behaviour that is not defined is undefined behaviour. Integer to pointer conversion is IDB, which is the same as UB except that a conforming compiler documents what could happen. I believe it's typically documented as unpredictable, so the same as plain UB.

➕ show 2 replies
imnotr0b0t • today at 4:41 PM

This is interesting