<- Back
Comments (91)
- quelsolaarThe argument that this is valid defined C is not entirely correct. You can not subtract one pointer form another that doesn't come from the same allocation, that is explicitly UB. But the user here subtracts one integer from the other and that is valid. However C only guarantees you can you can convert a integer to a valid pointer, if that integer has been converted from a pointer, and not been modified in any way. If the integer has been modified its implementation defined what happens. Its no longer valid standard C.C supports pointers that can have memory banks, or pointers that are consist of a hash and and an offset. Real world hardware architectures like CHERI does this and standard C supports this. In hardware like that, this would break, even if the subtraction is done on integers, and don't violate the no subtraction of pointer of different allocation rule. So no, Pointers are really not integers, even if they may look like integers on your machine.E (Member of the wg14 memory model study group.)
- tialaramexQuite some time after this blog was written Aria blogged about the "strict provenance experiment" for Rust, and today Aria's API (more or less) is sanctioned and Rust is defined to deliver the behaviour Aria described in the Tower of Weakenings.Here's the Rust documentation: https://doc.rust-lang.org/std/ptr/index.html#strict-provenan...So in Rust much of this is spelled out today, although of course the LLVM backend still miscompiles some programs (in Rust or indeed in C or C++) because it can be tricked into giving integers provenance even though that's clearly not what anybody wanted. Oops.However in C, which most of the examples are written in, the status is a bit less clear. Martin Uecker (who can often be found here in HN's comments) and colleagues wrote a document for ISO but that document is not (yet?) part of the ISO C standard itself. Of course in practice compilers like LLVM, GCC and MSVC need to do what the document says instead of making it up as they go, or else this matters very little.One thing which cannot be in doubt in any of these close-to-the-metal languages today, almost certainly including obscure ones you've maybe taken a shine to like Odin or Hare, is that provenance is a thing, if you're sure you don't have pointer provenance in your languages but you do have actual pointers, check with a stop watch. If your programs in that language are very slow you might be right.Because that's why this became a thing. Without provenance, pointers strangle many "obvious" optimisations. Add provenance and now these optimisations are available and your programs go much faster. It's a bargain - albeit one you probably never signed up for as a programmer.
- andaiArticle 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" ?
- notoranditAssembly 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.
- dapperdrakeThere was an old VAX or something that had a C standard library where function pointers in both C and Fortran were multiple machine words. C was 4 words and Fortran was 6 with everything but the first two being reserved for later. The idea was to provide additional argument and return type information in tag bits in the additional machine words.So, mathematically, pointers still are integers, because "everything" can be encoded as a natural number. Handles like in Windows or UEFI, hardware descriptors like internal to netmap and io_uring for NIC physical memory mapped control registers, 32-bit integer page numbers within sqlite databases, or memory addresses in x86_64 registers. Tagged numbers for counting stuff. Just look at off_t in Linux. Or unix filesescriptor fd numbers into a tiny per-process table of each Linux process. Some with hardware support, some without.And then stuff like ptrdiff_t in the C abstract machine.All the same idea. And it "only works", because they are "just numbers".And then come the implementation details.
- bryanlarsenPointers have provenance, integers don't.
- kibwenRecommended further reading in this blog series by Ralf Jung: https://www.ralfj.de/blog/2020/12/14/provenance.html
- hn_submitContext.
- pjmlp2018, and some of the comments are already outdated, SPARK 2014 is out there with ownership model.
- jojogeoOne is a mathematical construct, the other is a dog?
- nico> What's the difference between an integer and a pointer?This drove many people mad in college when learning CAnd to be fair, sometimes tests seemed like they were trying to deliberately make us sufferConceptually they seem pretty clear (value vs link/reference). I think most of the confusion arrises when implementing, C can be hard to read and cryptic to follow when running. A debugger is almost always needed for anything but the most basic projectsI wonder if the hello world for C should actually include debugging instructions
- glitchcI 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.
- stronglikedanwell, their names, for starters...
- m00dyrange of values
- anonundefined
- cowlevel> 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.
- imnotr0b0tThis is interesting
- spwa4There is no difference.Yes, I'm a C programmer, how did you know?