Sign in to follow this  
Japanese

What should I use NULL or 0

Recommended Posts

I know NULL has been has defined to 0. But some people say it's better if you use 0. We should not rely on NULL. Can anyone explain this?

Share this post


Link to post
Share on other sites
It's not 'better' in any way shape or form. Some people use null to be explicit and more obvious when setting pointers to 0. NULL is just #define NULL 0 anyways.

Share this post


Link to post
Share on other sites
Personally I use 0 because I feel it looks cleaner and better shows my intent. There can be some minor problems with NULL though, some compilers define it differently (0UL, (void*)0, 0 etc.) which might give you some problems on non-standard compilers. Apart from that I can't see any real problems NULL could give you.

Of course there is also the problem of clearly showing what you mean. Null is actually a value which should be used to represent a null-pointer. A null-pointer is a pointer which explicitly show that it doesn't have a target (not necessarily by being 0). So using NULL for anything but pointers might confuse some people.

Share this post


Link to post
Share on other sites
Quote:
Original post by DrEvil
NULL is just #define NULL 0 anyways.


This is the kind of stuff which can lead to confusion, the standard states:
Quote:
The macro NULL is an implementation-defined C++ null pointer constant in this International Standard (4.10). 180)

180) Possible definitions include 0 and 0L, but not (void*)0.

Share this post


Link to post
Share on other sites
So, what I interpret is-->
I can use 0 anywhere and if I have to use NULL then I should limit it to pointers only. Is that right.

Share this post


Link to post
Share on other sites
Yes you should definately only use NULL for pointers; not ints or anything. If a compiler gives you a problem with NULL, you can just redefine it to 0 anyway.

Share this post


Link to post
Share on other sites
Quote:
Original post by Japanese
So, what I interpret is-->
I can use 0 anywhere and if I have to use NULL then I should limit it to pointers only. Is that right.


What is promised is that that every pointer can be assigned to NULL, and NULL is zero (not necessarily 0, could be 0U, 0L etc.). So you're free to assign any pointer to NULL. NULL is an integral constant expression so you can also assign an integral (char, signed char, short int, int, long int and their unsigned variants) to NULL. In C++ (and C) NULL is guarenteed to be zero so you can depend on that behavior, but programmers from other backgrounds might be a little confused when you use NULL instead of 0. In general I wouldn't use NULL at all, but this is just personal preference, like Nitage said, if you're consistant then it really doesn't matter.

Share this post


Link to post
Share on other sites
I also think it is a good idea to use NULL for pointers.
A NULL shows me a variable holding a address, a 0 shows me a variable holding a number.

BTW 0 and NULL are realy the same, if there's a problem with your compiler just define a constant (like #define NULL 0) and you get along with your compiler <BG>.

MFG Damien K.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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'.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Assuming C++

Initializing to null:
type* pointer = 0;

Checking for non-null-value:
if (pointer) { ... }

Checking for null-value.
if (!pointer) { ... }

Share this post


Link to post
Share on other sites
Ah, it's that time of year again!

#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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Should I use NULL or 0?

-> http://www.research.att.com/~bs/bs_faq2.html#null

Share this post


Link to post
Share on other sites
Quote:
Original post by The Parrot
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.


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?

Share this post


Link to post
Share on other sites
On today's flat memory machines, NULL is defined as some form of 0. On segmented systems, where pointers are multicomponent, NULL can be anything, but usually some form of 0:0. (elder borland compilers defined it differently) The general rule for c is that 0 is a literal zero and NULL is a memory offset that will never be returned by malloc() or any similar call, so it can be used for marking a pointer as invalid or empty. For cpp it's more clearly defined but until the standard offers something more defined for null pointers, imho the best practice is to use NULL. The writer of the system dependent header file that contains its definition probably knows better what value should it have.

Viktor

Share this post


Link to post
Share on other sites
Quote:
Original post by KingsRevenge
However what happens when you type a variable name over 256 letters?

I never tried it.. I just read the number 256 somewhere. I imagine that should you type such a long name, the dev environment would throw an error. Considering the code is compiles, symbol names don't matter, but for the compiling itself there are limits everywhere (such as the 1024-include-depth limit).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this