3 ms·
> I prefer the simple and obvious pointer model. Vastly. That's great, but then you're not writing C anymore. When you write C (or indeed, almost any language
by Diggsey 6y ago
> I prefer the simple and obvious pointer model. Vastly.
That's great, but then you're not writing C anymore. When you write C (or indeed, almost any language in use today) you are writing code against an abstract machine, not against real hardware.
Pointers are integers on (most) real hardware. They are indisputably not integers in the C abstract machine.
The reason for this abstract machine existing is not to enable optimizations, it's to allow the code to be portable. If there was no abstract machine, you would have to reason about the correctness of your code for each target architecture independently. With an abstract machine, you can reason that your code is correct on the abstract machine, and the compiler can do the hard work of translating that to each target architecture in a way that preserves well-defined behaviour.
Now, given that the abstract machine exists, you could ask why it is so complicated, and the answer this time is for optimizations, and also to allow the translation to the target architecture to introduce as little overhead as possible.
- jasonhansel 6y agoAgreed. The "pointers as integers" model requires (among other things) that unrelated pointers be comparable with < and >, which IIRC is undefined behavior in the C spec because it has trouble with some exotic memory models.
- mhh__ 6y agoIt also doesn't really make sense in abstract consideration - is one address "bigger" than the other one?
- deleted 6y ago[deleted]
- jerome-jh 6y agoSince one can add to and subtract from a pointer, it is only natural to be able to compare them. And the standard clearly states pointers can be compared, although it requires the two pointers to point to the same array (or last element+1) or to the same structure.
- scottlamb 6y ago> Pointers are integers on (most) real hardware. I'm curious. Can you elaborate on "most"? The closest thing I know of to an exception is ARM's memory tagging extension stuff, [1] and I'm not sure if that qualifies because it looks like the actual checking of the tag is done by compiler-emitted assembly rather than automatically by the hardware. [2] I suppose it at least violates the idea that casting the pointer to an integer type doesn't change the value and still allows you to sort addresses via a simple <. [1] https://security.googleblog.com/2019/08/adopting-arm-memory-tagging-extension.html https://security.googleblog.com/2019/08/adopting-arm-memory-... [2] https://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html https://clang.llvm.org/docs/HardwareAssistedAddressSanitizer...
- Diggsey 6y agoThere's CHERI: https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/ https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/