5 ms·
You're mostly correct but to be pedantic, in C the literal value 0 does not actually mean address 0. The literal value of 0 is an implementation defined value t
by Kranar 3y ago
You're mostly correct but to be pedantic, in C the literal value 0 does not actually mean address 0. The literal value of 0 is an implementation defined value that represents a null pointer, but that value does not have to be address 0, it could be some other address.
This is significant because the following two snippets of code are not required to be equal in C.
char* c = (char*)(0);
char* d;
memset(&d, 0, sizeof(d));
c == d; // this does not have to be true
- flohofwoe 3y ago> that value does not have to be address 0, it could be some other address. That's what the spec says, but are there any computers around anymore where nullptr is not zero? The spec should really go with the times IMHO, old hardware can be supported via platform-specific language extensions.
- billyoneal 3y agoThe big place where it happens in modern environments are for pointers to members, where 0 is a bad nullptr value because the member at index 0 is legitimately a thing you want to name. MSVC uses -1 for nullptr in such cases.
- josefx 3y agoThe cleanest way would probably be to get rid of 0 as a null pointer constant, make its use in contexts that expect a pointer illegal and replace it with an explicit keyword. Of course that would break all existing code that uses 0 or (void*)0 instead of NULL, but if you are out to break things anyway you might as well get rid of 40 year old warts.
- gofixurcode 3y agoAh a new standard! Perfect! Everyone will adopt this as clearly it is better and there’s no reason not to right? … right?
- josefx 3y agoWell it should be backwards compatible if you just use NULL everywhere where it is appropriate, so you wouldn't even have to sprinkle ifdefs all over the place. Code that uses 0 all over the place would probably have to be updated with the help of compiler errors and a static code analyzer.
- Kranar 3y ago>That's what the spec says, but are there any computers around anymore where nullptr is not zero? Yeah that's absolutely fair. I actually can't think of a single platform, embedded or otherwise, old or new, where the null pointer is not address 0.
- camel-cdr 3y agoI've seen a compiler that has an alternative null pointer address, but I couldn't find it again.
- msla 3y agohttps://c-faq.com/null/machexamp.html https://c-faq.com/null/machexamp.html > Depending on the ``memory model'' in use, 8086-family processors (PC compatibles) may use 16-bit data pointers and 32-bit function pointers, or vice versa. And it isn't just zero anyway because there's segmentation in play.
- sargstuff 3y agoPerhaps a hascii take on the bending spoon in the matrix scene would be better *char reference.
- dekhn 3y agoHow do you reference literal address 0, then? From how you describe it sounds different from literal address 1, etc.
- sargstuff 3y agosomewhat covered in different hacker news post, for C, roughly: [] *()++ Technically, can only do address "1" with first bit set as a valid, usable address on hardware allowing addressing smaller than 8 bits (standard hardware epsilon factor). So really, address one is literally address 0 + epsilon, where epsilon is minimal addressable bit group, typically 8 bit clean without seg fault. Although, for standard x86, epsilon size would depend on which ring/boot method level & asm addressing method available (8,16,32,64).