Top
Best
New

Posted by germanrabbit 2 days ago

What's the difference between an integer and a pointer? (2018)(blog.regehr.org)
69 points | 39 comments
andai 1 hour ago|
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" ?

amavect 24 minutes ago|
>ultimately it's the same pattern of ones and zeroes

Exactly. Much equivocation happens when talking about concepts and representation of those concepts. Numbers, bitvectors, pointers, ones-and-zeros, colors, characters, indices, enum values, and whatever all share the same representation. The author thought "bitvector" best represented the commonality (though it doesn't).

notorandit 13 minutes ago||
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 30 minutes ago||
Recommended further reading in this blog series by Ralf Jung: https://www.ralfj.de/blog/2020/12/14/provenance.html
hn_submit 2 hours ago||
Context.
bryanlarsen 2 hours ago||
Pointers have provenance, integers don't.
dotancohen 2 hours ago||
Go on.
bryanlarsen 2 hours ago||
I tried, but frankly, typing "pointer provenance" into Google gets you a much better explanation than my attempt. Google's AI analysis might be questionable, but it's less questionable than my first attempt.

To oversimplify, provenance is the metadata a compiler keeps about a pointer during the compilation process.

c_engineer56 17 minutes ago||
For example, Arm Memory Tagging Extension uses the top byte of a 64-bit address to store a tag that the CPU verifies against the allocation tag on pointer dereference. If the tags don't match, an exception occurs. MTE makes exploiting memory vulnerabilities more difficult by preventing the attacker from generating valid pointers to hijack program control flow with. To bypass MTE, the attack would need to combine the exploit with an info leak or arbitrary memory read to set the correct tag for a new pointer.

On a MTE enabled system, adding an offset to a pointer cast to integer will result in an exception with high probability since the new pointer will be using the tag for the old address.

https://docs.kernel.org/arch/arm64/memory-tagging-extension....

pdlclay 48 minutes ago||
[flagged]
glitchc 2 hours ago||
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.
8-prime 2 hours ago||
The GCC compiler changes the order of the operations such that the assignment happens after the print for x.

call __printf_chk@PLT mov ecx, DWORD PTR 0[rbp] mov rdx, rbp mov edi, 2 mov DWORD PTR -96[rbx], 7

mkarrmann 1 hour ago||
The "special" thing of note is that GCC considers it acceptable to change the semantics of this program depending on the optimization level.
wat10000 1 hour ago||
That depends entirely on how you define “the semantics.” Any optimizing compiler has to draw a line between what the program is meant to do and how the emitted code actually makes it happen. The proper location of that line is not obvious. Can it turn x++,x++ into x+=2? That’s normally fine, potentially disastrous if x is a memory mapped register on an external device.

C and C++ happen to have a particularly restrictive view on what “the semantics” of a program really are, such that there are a lot of things you can write in syntactically-legal C that officially erase the semantic meaning of the code. Within that definition of the program’s semantics, gcc’s optimizer is not supposed to change them, and any such change would be a bug. You’re taking a more expansive view of program semantics than the actual language specifies. If you do that, then any optimizing compiler is going to consider it to be acceptable to violate your special bespoke definition of them.

marcosdumay 1 hour ago||
> The proper location of that line is not obvious.

If that's true, you have a bad language.

What is known to be the case with C, but this is not a desirable nor a necessary state of affairs. A language can and should have defined semantics.

pjmlp 3 hours ago||
2018, and some of the comments are already outdated, SPARK 2014 is out there with ownership model.
jojogeo 1 hour ago||
One is a mathematical construct, the other is a dog?
stronglikedan 2 hours ago||
well, their names, for starters...
spwa4 2 hours ago|
There is no difference.

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

HarHarVeryFunny 30 minutes ago||
C doesn't provide any guaranteed 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.

Ohentis 2 hours ago|||
It doesn't sound like you are or you would know C doesn't define either precisely enough to make that statement true.
Maxatar 1 hour ago|||
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.

kevin_thibedeau 1 hour ago|||
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.
cryptonector 47 minutes ago||
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.
yxhuvud 2 hours ago|||
There is a counter example in the OP.
Ungh 2 hours ago|||
There's the voice of a C programmer who hasn't used hardware with a different word size to address space...
AnimalMuppet 1 hour ago||
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.

cryptonector 45 minutes ago|||
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.
Ungh 50 minutes ago|||
Segmented 8086 is always the fun one.
coolius 1 hour ago|||
I agree, I have passed void* to the JVM by casting to int more often than I can count
drfloyd51 2 hours ago||
You should read TFA.
More comments...