What is the difference between NULL and nullptr when using them for something like a Binary Search Tree? Are those interchangeable?
Is C NULL equal to C++11 nullptr - Stack Overflow
Trying to understand NULL pointers
Using Null pointers - C++ Forum
Videos
As far as I know, NULL is just another name for 0 (edit 3: I used the NULL from <iostream>)for which nullptr is a pointer pointing 0. Some references I have used NULL, but others have nullptr. They are writing their trees in different ways so I am not sure if they are interchangeable, but I saw someone on an overflow post saying that versions of C++ do consider NULL == nullptr. But my past experiences are saying that int *some operator here* pointer is a mess. Can anyone clear this up for me? Thank you!
edit:sample for NULL:TreeNode *leftPtr = NULL;TreeNode *rightPtr = NULL;
sample for nullptr:TreeNode *leftPtr = nullptr;TreeNode *rightPtr = nullptr;// idk if these work, or if they only work if:// for example rootPtr->rightPtr = nullptr
// edit 2: running this, it prints "1" so it must be true,
#include <iostream>
using namespace std;
int main() {
cout << (NULL == nullptr) << endl;
return 0;
}
Hello all again,
I have another stupid question here lol, so I'm trying to wrap my head around NULL. Im currently under the impression that NULL is a built in constant that has a value of zero, but what does that actually mean? When would it be appropriate to use null? If someone could explain it in layman's terms that would be super helpful!
How is it a keyword and an instance of a type?
This isn't surprising. Both true and false are keywords and as literals they have a type ( bool ). nullptr is a pointer literal of type std::nullptr_t, and it's a prvalue (you cannot take the address of it using &).
4.10about pointer conversion says that a prvalue of typestd::nullptr_tis a null pointer constant, and that an integral null pointer constant can be converted tostd::nullptr_t. The opposite direction is not allowed. This allows overloading a function for both pointers and integers, and passingnullptrto select the pointer version. PassingNULLor0would confusingly select theintversion.A cast of
nullptr_tto an integral type needs areinterpret_cast, and has the same semantics as a cast of(void*)0to an integral type (mapping implementation defined). Areinterpret_castcannot convertnullptr_tto any pointer type. Rely on the implicit conversion if possible or usestatic_cast.The Standard requires that
sizeof(nullptr_t)besizeof(void*).
Why nullptr in C++11? What is it? Why is NULL not sufficient?
C++ expert Alex Allain says it perfectly here (my emphasis added in bold):
...imagine you have the following two function declarations:
Copyvoid func(int n); void func(char *s); func( NULL ); // guess which function gets called?Although it looks like the second function will be called--you are, after all, passing in what seems to be a pointer--it's really the first function that will be called! The trouble is that because NULL is 0, and 0 is an integer, the first version of func will be called instead. This is the kind of thing that, yes, doesn't happen all the time, but when it does happen, is extremely frustrating and confusing. If you didn't know the details of what is going on, it might well look like a compiler bug. A language feature that looks like a compiler bug is, well, not something you want.
Enter nullptr. In C++11, nullptr is a new keyword that can (and should!) be used to represent NULL pointers; in other words, wherever you were writing NULL before, you should use nullptr instead. It's no more clear to you, the programmer, (everyone knows what NULL means), but it's more explicit to the compiler, which will no longer see 0s everywhere being used to have special meaning when used as a pointer.
Allain ends his article with:
Regardless of all this--the rule of thumb for C++11 is simply to start using
nullptrwhenever you would have otherwise usedNULLin the past.
(My words):
Lastly, don't forget that nullptr is an object--a class. It can be used anywhere NULL was used before, but if you need its type for some reason, it's type can be extracted with decltype(nullptr), or directly described as std::nullptr_t, which is simply a typedef of decltype(nullptr), as shown here:
Defined in header <cstddef>:
See:
- https://en.cppreference.com/w/cpp/types/nullptr_t
- and https://en.cppreference.com/w/cpp/header/cstddef
Copynamespace std
{
typedef decltype(nullptr) nullptr_t; // (since C++11)
// OR (same thing, but using the C++ keyword `using` instead of the C and C++
// keyword `typedef`):
using nullptr_t = decltype(nullptr); // (since C++11)
} // namespace std
References:
- Cprogramming.com: Better types in C++11 - nullptr, enum classes (strongly typed enumerations) and cstdint
- https://en.cppreference.com/w/cpp/language/decltype
- https://en.cppreference.com/w/cpp/types/nullptr_t
- https://en.cppreference.com/w/cpp/header/cstddef
- https://en.cppreference.com/w/cpp/keyword/using
- https://en.cppreference.com/w/cpp/keyword/typedef