tags:

views:

187

answers:

5
+2  Q: 

Using NULL in C++?

Possible Duplicate:
Do you use NULL or 0 (zero) for pointers in C++?

Is it a good idea to use NULL in C++ or just the value 0?

Is there a special circumstance using NULL in C code calling from C++? Like SDL?

A: 

From crtdbg.h (and many other headers):

#ifndef NULL
#ifdef __cplusplus
#define NULL    0
#else
#define NULL    ((void *)0)
#endif
#endif

Therefore NULL is 0, at least on the Windows platform. So no, not that I know of.

Konrad
+1  A: 

I never use NULL in my C or C++ code. 0 works just fine, as does if (ptrname). Any competent C or C++ programmer should know what those do.

T.E.D.
Since I seldom even use pointers (they are all inside smart pointers) it makes little difference.
Martin York
+5  A: 

In C++ NULL expands to 0 or 0L. See this comment from Stroustrup:

Should 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.

Tarantula
I think I have to disagree with Stroustrup on the last bit. I'd avoid use of nullptr in systems in which it isn't already implemented for the exact reason that it will later. Behavior of the nullptr keyword cannot be replicated with an integral definition. I don't think it could be replicated at all. You could run into situations where your code does one thing one day and then the next, after a compiler upgrade, something totally different. Essentially, everything the 'nullptr' keyword is meant to address will change.
Noah Roberts
Thanks, man! This really helped. Would #define nullptr 0 be of any help?
Jookia
@Jookia: No! Do not use `#define nullptr 0` in your code, read Noah Roberts' comment above. If you do that, when you do upgrade to a C++0x compliant compiler your code might either not compile (which would be a good thing) or do weird stuff.
Praetorian
Also, a good reason to keep using NULL instead of 0 in your code is that when you do upgrade to a compiler that supports the `nullptr` keyword, searching and replacing "NULL" will be a lot easier than doing the same for "0".
Praetorian
@Noah @Jookia @Praetorian: [`nullptr` is simple to implement](http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/nullptr). If you want, make a `nullptr.hpp` file, include the definition for it, and use it. When C++0x comes along, just delete the include and include-directives.
GMan
Stroustrup's advice here is predicated on the idea that macros are bad. Which is true. But not being able to tell the difference between integer comparisons and pointer comparisons is bad. Not being able to search for pointer comparisons is bad. For me, these bads are worse then trying to avoid the macro NULL.
Bill
@GMan - Well, I stand corrected in my third sentence. I'd say if you're going to use nullptr then to do it that way and not just defining it to 0. The later will have more behavioral changes.
Noah Roberts
A: 

The downside of NULL in C++ is that it is a define for 0. This is a value that can be silently converted to pointer, a bool value, a float/double, or an int.

That is not very type safe and has lead to actual bugs in an application I worked on.

Consider this:

void Foo(int i);
void Foo(Bar* b);
void Foo(bool b);


main()
{
     Foo(0);         
     Foo(NULL); // same as Foo(0)
} 

C++0x defines a nullptr that is convertible to a null pointer but not to other scalars. VC++ supports this from 2008 (could be earlier). GCC has had something like this for ages but it is there called __null.

jdv
A: 

Assuming that you don't have a library or system header that defines NULL as for example (void*)0 or (char*)0 it's fine. I always tend to use 0 myself as it is by definition the null pointer. In c++0x you'll have nullptr available so the question won't matter as much anymore.

Mark B