3 ms·
> you cannot pass a pointer-sized value when you tell printf() you're passing an integer. Actually you can. You probably shouldn't, particularly now that many
by mpweiher 5y ago
> you cannot pass a pointer-sized value when you tell printf() you're passing an integer.
Actually you can. You probably shouldn't, particularly now that many 64 bit C machine models have different sized pointers and ints. If they were going to use an int conversion, they should have used %ld.
> You should use %p which is for printing pointers,
Agreed.
> [no nasal daemons] That's just being lucky,
Actually it isn't "just lucky". Well, to be precise it wasn't just lucky back when C compiler writers didn't stretch the idea of UB way beyond the breaking point, and way beyond certainly the intent and arguably the letter of the standard.
(The cop out is that the part of the standard that explicitly says all these shenanigans are not OK was made non-binding in later versions of the standard, it originally was binding, and thus people pretend that this part of the standard doesn't exist at all. But it does.)
> There is no guarantee that the in-memory representation of a NULL pointer is "all bits zero",
There is no guarantee of this in the C standard. However, the C standard is intentionally incomplete, i.e. conformance to the standard is not enough for an implementation to be "fit for purpose". I think it even used to say that somewhere in the standard.
> I realize that in practice, on common hardware, NULL is typically implemented as all-bits-zero,
For a very not to say overwhelmingly large value of "typically". I can't name a single implementation that does not, and my guess would be that total market share of such implementations would be less than 0.1%, and I believe I am being very conservative with that estimate. Which also means that if you are on such an architecture, you almost certainly know it.
> but that is not defined by the language.
...because the language definition is intentionally incomplete. People talk about the "C abstract" machine as if it were a thing. It's not. C maps to real hardware, that's the point of the language, and so taking the real-world behavior of the hardware that you are going to run on into account is perfectly fine.
- SAI_Peregrinus 5y agoAndroid on 64-bit ARM uses the MSB of pointers for a tag. There are 2^8 NULL pointer values at the hardware level there.
- mpweiher 5y agoInteresting! Do we actually need any of them apart from all zeros? Are the tags retained when converting?