What should I use NULL or 0
Like KingsRevenge, I use 0 pretty much all the time, since it's a few less keystrokes to type, and it's just as easy to read and understand for a human.
Quote:Original post by KingsRevenge
If your going to school you may want to use NULL for pointers. My teacher marked a poitn off my test becasue I used a zero instead of NULL because its three less letters to write.
That sounds like a priority problem on your teachers side - and a rather daft one, at that.
I use it only for clarity. In a constructor;
MyClass::MyClass(): PointerToOtherClass(0){}
(to make sure the pointer actually is 0 and not 'CDCDCD' or some of that kind). For checks in longer bits of code:
if (NULL != MyPointer){ MyPointer->Flush();}
The first is part of a complex statement where you need to know what you're doing; the second is more of a 'fix-a-bug' type of fix, where 'NULL' is a signal for me that I'm doing a pointer check. I never use NULL to initialize an number.
As with any convention, it depends on what works for you or the ones you have to work with. Saying 'NULL' is wrong because '0' is shorter is absolutely silly and it's the kind of thing that makes people call functions "PtrRtrnVluOver(BVa a1, BVa a2)". Which is OK if you understand it, but in my eyes the more clear the code is, the less of it needs to be in your head at the same time; lower upkeep value. The symbols in VC++ have a maximum length of 256 characters, even!
So do what you like :) I used to tab my '{' inwards and use Hongarian, until I realised that the first was just confusing for others and the later was just a pain in all manner of nasty places.
I always use NULL for pointers and 0 for variables as it avoids confusion. If I see a NULL I know I'm untargeting a pointer, if I see a 0 I know I'm resetting a variable.
Some people advocate using 0 instead of NULL for three main reasons that I've heard -
In the early days of C++ the definition of NULL was taken from the C definition which is wrong for C++. This reason is pretty much obsolete but people keep using it anyway.
NULL is implemented as a macro and thus requires the preprocessor. Some people take the "the preprocessor is evil" mantra a little to far.
0 is easier (fewer keystrokes) to type. This is just silly IMHO. If the time spent type 0 instead of NULL makes *any* difference in any real situation I'd be amazed.
I prefer NULL simply because it gives more context. If you see NULL in my code it means I'm playing with pointers. Similiarly the thing on the end of a C-style string isn't NULL or 0 it's '\0'.
In the early days of C++ the definition of NULL was taken from the C definition which is wrong for C++. This reason is pretty much obsolete but people keep using it anyway.
NULL is implemented as a macro and thus requires the preprocessor. Some people take the "the preprocessor is evil" mantra a little to far.
0 is easier (fewer keystrokes) to type. This is just silly IMHO. If the time spent type 0 instead of NULL makes *any* difference in any real situation I'd be amazed.
I prefer NULL simply because it gives more context. If you see NULL in my code it means I'm playing with pointers. Similiarly the thing on the end of a C-style string isn't NULL or 0 it's '\0'.
In many languages, you HAVE to use null. Don't fart around with types because C doesn't care. NULL is #define'd for a reason. And that reason is to show that you are testing a pointer (or a reference in other languages) versus testing an integer.
You should use NULL for the same reason that you use true/false instead of 1/0. It makes your intentions more explicit. It doesn't make code any slower, and it makes it MUCH harder to screw up other people reading your code.
You should use NULL for the same reason that you use true/false instead of 1/0. It makes your intentions more explicit. It doesn't make code any slower, and it makes it MUCH harder to screw up other people reading your code.
Assuming C++
Initializing to null:
Checking for non-null-value:
Checking for null-value.
Initializing to null:
type* pointer = 0;
Checking for non-null-value:
if (pointer) { ... }
Checking for null-value.
if (!pointer) { ... }
Ah, it's that time of year again!
You may debate the wisdom of using the name "nullptr", which has been proposed as a new keyword for the next C++ standard.
Σnigma
#include <iostream>class NullPtr{ public: template < typename TYPE > operator TYPE *() const { return 0; } template < typename TYPE, typename CLASS > operator TYPE CLASS::*() const { return 0; } private: void operator&() const;};NullPtr const nullptr;void overloaded_function(int){ std::cout << "overloaded_function(int) called\n";}void overloaded_function(char *){ std::cout << "overloaded_function(char *) called\n";}int main(){ // prints "overloaded_function(int) called" overloaded_function(0); // prints "overloaded_function(int) called" overloaded_function(NULL); // prints "overloaded_function(char *) called" overloaded_function(nullptr);}
You may debate the wisdom of using the name "nullptr", which has been proposed as a new keyword for the next C++ standard.
Σnigma
I tend to stick with 0. If I'm directly interfacing with the Win32 API I sometimes throw in NULLs for pointers, though - those 20 argument functions do get some benifit. Comments would be even better though in those cases, and it's usually self evident in others :-).
Quote:Original post by The ParrotQuote:Original post by KingsRevenge
If your going to school you may want to use NULL for pointers. My teacher marked a poitn off my test becasue I used a zero instead of NULL because its three less letters to write.
That sounds like a priority problem on your teachers side - and a rather daft one, at that.
I use it only for clarity. In a constructor;MyClass::MyClass(): PointerToOtherClass(0){}
(to make sure the pointer actually is 0 and not 'CDCDCD' or some of that kind). For checks in longer bits of code:if (NULL != MyPointer){ MyPointer->Flush();}
The first is part of a complex statement where you need to know what you're doing; the second is more of a 'fix-a-bug' type of fix, where 'NULL' is a signal for me that I'm doing a pointer check. I never use NULL to initialize an number.
As with any convention, it depends on what works for you or the ones you have to work with. Saying 'NULL' is wrong because '0' is shorter is absolutely silly and it's the kind of thing that makes people call functions "PtrRtrnVluOver(BVa a1, BVa a2)". Which is OK if you understand it, but in my eyes the more clear the code is, the less of it needs to be in your head at the same time; lower upkeep value. The symbols in VC++ have a maximum length of 256 characters, even!
So do what you like :) I used to tab my '{' inwards and use Hongarian, until I realised that the first was just confusing for others and the later was just a pain in all manner of nasty places.
Yea, I understand personally I could careless, I use either or depending on where they fit best but I mostly opt for NULL when using pointers. However what happens when you type a variable name over 256 letters?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement