4 ms·
Another problem with that typedef and usage is that it assumes that NULL as an integer is 0. It need not be. The literal constant 0 cast to or assigned to a poi
by tzs 13y ago
Another problem with that typedef and usage is that it assumes that NULL as an integer is 0. It need not be. The literal constant 0 cast to or assigned to a pointer type always works because they compiler treats that as a special case, and replaces the 0 with whatever the actual NULL bit pattern for that pointer type. E.g., "char * p = 0" might actually generate something like "move #0xFFFFFFFF, p" on a machine where 0xFFFFFFFF is the bit pattern for NULL char pointers.
A lot of people forget this, and think that NULL pointers must actually have a bit pattern that is all zeros.
- joosters 13y agoName me a common system where NULL != 0 You might as well be worrying about code compatibility with systems that have a 7 bit char, or some other irrelevance.
- MaulingMonkey 13y agoC++ member pointers are frequently represented as offsets into the class on vanilla x86. Given that 0 is an entirely valid offset -- the first member given a class without initial padding or vtable -- NULL must of course be represented by an entirely different, nonzero, bitpattern.
- icedchai 13y agohmm? you're adding an offset to a pointer (the base of the object), which is still not going to equal 0 even if the offset is 0.
- MaulingMonkey 13y agoThe C++ standard calls pointers to members... well, pointers to members. I'm not offsetting anything: The compiler is on my behalf. It could create function stubs for all I care, similar to how one might emulate this feature in C#. #include <iostream> struct Foo { int bar; }; int main() { int (Foo::*null_pointer) = nullptr; int (Foo::*first_member_pointer) = &Foo::bar; std::cout << *(std::ptrdiff_t*)&null_pointer << "\n"; std::cout << *(std::ptrdiff_t*)&first_member_pointer << "\n"; } Running this code after compiling it with VS2012 results in, on my machine: -1 0
- deleted 13y ago[deleted]
- eeadc 13y agoThat's simply not true. There is the rule, that NULL must be equal to (void *)0. I had also several discussions about that topic, but it's part of at least C99 and C11.
- tzs 13y agoYes, (void * )0 gives a null pointer. I mentioned that. However, that doesn't mean all the bits of a null pointer are required to be all 0. The actual machine representation of a null pointer is implementation-defined. The only requirement C99 places on the actual value of a null pointer is that it compares unequal to a pointer to any object or function, and any two null pointers compare equal. See section 6.3.2.3 paragraphs 3 and 4: An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function. Conversion of a null pointer to another pointer type yields a null pointer of that type. Any two null pointers shall compare equal. If you do this: int pv = 0; char * p = (char *)pv; you are not guaranteed to have p set to a null pointer, because the zero you are casting and assigning is not an integer constant zero. The behavior is implementation-defined, as is going the other way. Section 6.3.2.3 paragraphs 5 and 6: An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
- awda 13y agoThis should be okay, though: char *p1 = 0; int pv = (intptr_t)p1; char *p2 = (char *)pv; I.e. NULL must round-trip through intptr_t.