Suppose we have the variable declaration double * ptr;
What, if any, is the difference between the assignments:
ptr = 0;
and
ptr = NULL;
Suppose we have the variable declaration double * ptr;
What, if any, is the difference between the assignments:
ptr = 0;
and
ptr = NULL;
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
There might be no difference at all, since NULL is an implementation defined null pointer constant (that is not (void*)0), and by definition a null pointer constant is "an integral constant expression rvalue of integer type that evaluates to zero".
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Actually, not according to Stroustrup:
Stroustrup: C++ Style and Technique FAQShould I use NULL or 0?
In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.
If you have to name the null pointer, call it nullptr; that's what it's going to be called in C++0x. Then, "nullptr" will be a keyword.
You can signify an unset/invalid std::function and boost::shared_ptr (and other's as well I'm sure) by setting them 0. Are we then going to be setting them to nullptr? That seems kind of weird since they aren't pointers themselves. If we can't, then we'll be using nullptr in some parts, and NULL/0 in other parts - Yay (sarcasm). I prefer 0 since I think of a pointer as a variable to a point in memory, and 0 as a invalid point (you'd never use). I also think of 0 as a non-successful value (false), versus a positive value (true), so conditionally it makes sense. With all of the technicalities of C/C++ I don't understand why using 0 as null, or even NULL, is a certain and needs replacing. Something set to NULL is obvious, and does the job. Can even macro/typedef your own nullptr, nullfunc, nullobj to 0, or whatever.
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
In what way do you think Stroustrup contradicts my statement?Originally Posted by EVOEx
The problem is that you can set a non-pointer to NULL, since NULL evaluates to zero. This is precisely what nullptr is supposed to avoid.Originally Posted by Dae
Last edited by laserlight; 12-20-2009 at 12:32 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Trying to summarize:
1) NULL is just 0 for pointers (but used to have some different, and bad, implementations)
2) You can always use 0 in place of NULL
3) Since NULL is a macro, 0 might save some computational time
4) Stroustrup uses 0
5) NULL does have a symbolic difference, similar to that between '\0' and 0, but has some rough edges in the correlation (dissimilar to the '\0' correlation to 0).
That makes it sound awfully tempting to eliminate NULL from one's active vocabulary...
Well, yes, NULL is a null pointer constant, though "0 for pointers" sounds a little strange.Originally Posted by Aisthesis
Yes.Originally Posted by Aisthesis
No, other than the time spent by the preprocessor in replacing NULL with the actual null pointer constant, but this is very negligible and does not matter in the end.Originally Posted by Aisthesis
I am not sure what you mean by this. The advantage of using NULL over 0 is that it is clear that it is a null pointer constant. The disadvantage of using NULL instead of 0 is that this null pointer constant can be used in a context that has nothing to do with pointers (though apparently some compilers may be able to detect such cases and issue warnings).Originally Posted by Aisthesis
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
NULL might not be 0. For example, it could be 0L. Either way, it evaluates to zero.Originally Posted by EVOEx
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Sounds like one of those "How many angels can dance on the head of a pin?" questions....the answer to which is only as valid as someone else's opinion. I tend to agree with laserlight but I am sure that opinion will horrify some of the purists so I am stepping around this particular road apple...
Happy Sunday all.
J.
That's what I meant. You can also use 0 instead of '\0', but the latter indicates the null terminator more clearly.
Well, that gives NULL a bit of an edge, then. No real downside but greater clarity.
But it looks like the new release will introduce nullptr if I'm understanding various posts in this thread correctly. No way they'll deprecate NULL, though, pretty much ever. Way too much code already uses it.
I'm surprised this hasn't been mentioned yet.
The problem with NULL is that it usually defined as 0 and therefore isn't a pointer type. Therefore it is possible to use it in a context as an integers, and worst of all, it can be used in cases where an integer or a pointer is allowed, such as overloaded functions. In such cases it will generally be taken to be an integer. Using 0 instead of NULL in such cases makes this error more apparent, because a programmer reading the code will not be tempted to think that a pointer is being passed.
For this reason, I suggest using 0 instead of NULL in C++. (But not in C)
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.