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;
}
Flocked straight here because the toxic stack overflow has the biggest trigger finger when it comes to marking posts as duplicates.
But anywho.
I had an examination recently, and I remember one of the theory questions asking what the difference is between null and nullptr in c++. I was unsure and didn't know how to answer it, for future reference could someone please explain to me what differs between the two.
If we want to "Nullify" something why dont we create a pointer in a controlled way that points to value 0?
The cnullptr madness was to combat compatibility between C and C++ initial NULL and nullptr but then C23 had to pull a nullptr of its own so now it's C11 - C23 and C++ 🧐
Does this make any sense, FYI I've added few other things such as unused and nullify for additional roasting.
https://github.com/fossillogic/fossil-sys/blob/main/code/logic/fossil/sys/cnullptr.h
I am unsure what the difference is as null and 0 should be the same on bit level. Are there any real differences seen from the hardware or are they just renaming the same value?
Further, when should I use null if nullptr exists?
NULL is usually a preprocessor macro defined as 0, so they are indeed interchangeable. There are some archaic implementations that used something different, see here for examples.
With the advent of nullptr, both NULL and 0 (as a pointer value) should be considered obsolete. nullptr clearly expresses your intent, is platform independent (given C++11 support) and can't be accidentally assigned to an integer or float variable.
Also, see here for Bjarne Stroustrup's take on this topic.
0 is two things at the same time: the integer zero, and the null pointer constant. But that does not mean that the actual in-memory representation of the null pointer (i.e. the null pointer value) has to be all-zero-bits. It can be any value, as long as no valid pointer can take on that value. But whatever that value is, a pointer with that value must compare equal to 0, because that is the null pointer constant. On a fictional system where the null pointer value was 0xdeadbeef, then writing void *foo = 0; would have to assign the value 0xdeadbeef to foo, and foo == 0 would evaluate to true if foo had the value 0xdeadbeef.
See, it's all awfully confusing because this 0 serves double duty as two completely unrelated things. If we just said that the null pointer constant had a name like nullptr, and that the null pointer value was unspecified (as long as a valid pointer can never take on that value), then there would be no confusion whatsoever.
Not sure whether this is more appropriate for r/cpp, but I thought I'd ask here first.
I always use nullptr over NULL, for the reason that overload resolution with NULL can lead to surprising outcomes because it's an integer, and not a pointer. (also it's shiny and "modern", or it can be considered more idiomatic C++, I guess)
So I'm working with a new team member who is not convinced. He thinks this reason is really obscure and that you will rarely, if ever, encounter a real life scenario where that reason comes into play. I couldn't come up with an organic scenario that could happen in real code, and to be honest - I don't think I've seen something like that in the wild.
Would you insist on strictly using nullptr in your codebase? I keep seeing him use NULL in his pull requests and I'm starting to wonder if I should stop being the "code police" and give up on this battle.
I'm sorry if this question is stupid or trivial.
We’ve got NULL, which is an int 0 but not all bits 0 (how is that???), an ASCII ‘\0’ NUL, which apparently converts to NULL automatically but is somehow different, and then there’s std::nullptr_s etc. What’s the difference? Why can’t we have just one 0 instead of all these NULs, NULLs, 0s, all-bit-zeros, etc.?
Can anybody explain the intuition behind the concept of the NULL pointer and the rest of the 0s? Why isn’t there just one zero that works everywhere and means the same thing?
nullptr has type std::nullptr_t. It's implicitly convertible to any pointer type. Thus, it'll match std::nullptr_t or pointer types in overload resolution, but not other types such as int.
0 (aka. C's NULL bridged over into C++) could cause ambiguity in overloaded function resolution, among other things:
f(int);
f(foo *);
(Thanks to Caleth pointing this out in the comments.)
You can find a good explanation of why it was replaced by reading A name for the null pointer: nullptr, to quote the paper:
This problem falls into the following categories:
Improve support for library building, by providing a way for users to write less ambiguous code, so that over time library writers will not need to worry about overloading on integral and pointer types.
Improve support for generic programming, by making it easier to express both integer 0 and nullptr unambiguously.
Make C++ easier to teach and learn.
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!