5 ms·
Nulls in strongly typed languages can get rather weird but from a C/C++ perspective it is the same as 0. nullptr is just a correctly casted 0.
by edoo 8y ago
Nulls in strongly typed languages can get rather weird but from a C/C++ perspective it is the same as 0. nullptr is just a correctly casted 0.
- saagarjha 8y agonullptr is actually "a prvalue of type std::nullptr_t" or something like that. Since C++11 NULL is the same as nullptr.
- edoo 8y agoAnd it is directly convertible to a bool. The primary effectiveness seems to be with operator overloading where a NULL could trigger an integer method instead of a pointer method.
- ape4 8y agoYes that's the example given here. https://en.cppreference.com/w/cpp/language/nullptr https://en.cppreference.com/w/cpp/language/nullptr So its an improvement but not exactly a game changer.
- saurik 8y agoTo the small extent to which "since C++11" is even relevant to "from a C/C++ perspective", even in C++11 NULL is still allowed to simply be defined as 0.
- jcelerier 8y agoActually nullptr is a struct.
- trhway 8y agoreminds that https://en.wikipedia.org/wiki/Blind_men_and_an_elephant https://en.wikipedia.org/wiki/Blind_men_and_an_elephant :)
- grive 8y agoThis is only true in C, which is part of why C is less insane than C++. The two languages should not be conflated anymore.
- mbel 8y agoThis is not even true in C. 0 is a "null pointer literal" when used in pointer context, this does not imply that the actual null pointer has a value of zero.
- grive 8y agoAh yes you are right, an interesting point however, 6.3.2.3 Pointers 3. 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. 4. Conversion of a null pointer to another pointer type yields a null pointer of that type. Any two null pointers shall compare equal. Looking at the definition of NULL: 7.17 Common definitions <stddef.h> 3 The macros are NULL which expands to an implementation-defined null pointer constant; Meaning that NULL is implementation defined, but is assured by the standard to compare equal to any 0-valued pointer of any type, even if it is itself another special value.
- kps 8y agoSaying “equal to any 0-valued pointer” there can be misleading. It is true of a pointer assigned from a (foo*)0 constant, but not true of a pointer to a hardware address 0 or a pointer with bits all 0 (assuming they exist).
- masklinn 8y agoThat's not correct. In C, the literal "0" is a null pointer constant handled at the compilation stage, but casting a runtime zero is not specified to yield a null pointer (and the address zero can be perfectly valid and usable), nor are null pointers specified to be zero-valued (quite the opposite).
- edoo 8y agoI disagree. nullptr is directly convertable to a boolean 0: #include <stdio.h> int main(int argc, char *argv[]) { printf("%d\n", nullptr == 0); return 0; }
- masklinn 8y ago1. You're comparing a null pointer literal with a null pointer constant. You're literally comparing two things guaranteed to be equal. How, exactly, is it surprising that they do? Did you even attempt to understand what I wrote? 2. The specific implementation you have on hand could use zero-valued null pointers, I'm telling you what the standard doesn't say, confirmed by the C FAQ: http://c-faq.com/null/varieties.html http://c-faq.com/null/varieties.html http://c-faq.com/null/confusion4.html http://c-faq.com/null/confusion4.html http://c-faq.com/null/machexamp.html http://c-faq.com/null/machexamp.html 3. The null pointer is not "directly convertible to a boolean 0", it's the literal 0 which expresses a null pointer: http://c-faq.com/null/ptrtest.html http://c-faq.com/null/ptrtest.html
- edoo 8y agoI see what you are saying. I mean zero as null constant. I've never even seen anyone try to assign a null value with some integer zero at some memory address. Using the NULL constant in C/C++ is near effortless and 0 just works no matter what the underlying implementation is doing. So for all intents and purposes nullptr is just a casted NULL (0) constant.