17

Nowadays, with C++11, Whats recommended to use, Zero or NULL? The first of the second if?

int * p = getPointer(); if( 0 == p ){ // something } if( NULL == p ){ // something } 

UPDATE: I forget the new

if( nullptr == p ){ // something } 

UPDATE 2: the examples are to show the options to write null pointer, I know is more pleasant to write if( !p ).

3
  • 13
    Yoda Conditions look funny! Commented Oct 26, 2012 at 17:45
  • Remember the difference between null and 0... 0 is an integer, where as null means "nothing" - hence why you should use nullptr (and why 0ptr doesn't exist) Commented Oct 26, 2012 at 17:47
  • 3
    @VBAssassin: 0 as a literal is a particular beast, as it is both an integer and 'something' that converts to a null pointer. When used as a pointer, 0 is a null pointer. Note that NULL is defined in C++ to be a literal 0 (integer type) unlike in C where it is (void*)0. Of course the better option in C++11 is nullptr, but in C++03 I prefer 0 to NULL. Commented Oct 26, 2012 at 18:10

4 Answers 4

30

The other answers are right. But I wanted to say a little more about why nullptr is better.

In C++11 "perfect forwarding" is very important. It is used everywhere. Obvious places are bind and function. But it is also used in a multitude of other places under the covers. But "perfect forwarding" isn't perfect. And one of the places it fails is null pointer constants.

template <class T> void display(T) { std::cout << type_name<T>() << '\n'; } template <class T> void f(T&& t) { display(std::forward<T>(t)); // "perfectly forward" T } int main() { f(0); f(NULL); f(nullptr); } 

With an appropriate definition of type_name<T>(), on my system this prints out:

int long std::nullptr_t 

This can easily make the difference between working code and errors. With any luck your errors will come at compile time (with horrible error messages). But you may also get run time errors in some circumstances.

Aggressively ban use of 0 and NULL in your code.

Even if you're not perfect forwarding in your code, code you call (such as the std::lib) is very likely using it under the covers.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I used to write 0 always (I was convinced by Stroustop books), and NULL seems to my like red herring, But nowadays still is seen in multiple places. I'll get used now to write nullptr.
Note that GCC has the option to warn with -Wzero-as-null-pointer-constant. "Warn when a literal '0' is used as null pointer constant. This can be useful to facilitate the conversion to nullptr in C++11." gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
But in modern C++ implementations NULL is defined as nullptr, isn't it? Then why explicit nullptr is better than NULL? Or maybe this answer is simply outdated?
30

Neither, it's nullptr.

Though, in your case, I'd just go with

if ( !p ){ //something } 

2.14.7 Pointer literals [lex.nullptr]

1 The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t. [ Note: std::nullptr_t is a distinct type that is neither a pointer type nor a pointer to member type; rather, a prvalue of this type is a null pointer constant and can be converted to a null pointer value or null member pointer value.

6 Comments

You inversed condition. That's why I think it's better to write p == nullprt and p != nullptr
+1 after Googling up what nullptr actually meant :) This was a new one to me, but a very useful one.
+1 for !p. I find if(p) and if(!p) much easier to read than the more wordy alternatives.
if (p) cannot be used to check if a pointer is not null. because it casts to bool, thus potentially loose precious most significant bits. And only would you say otherwise if you have been tainted by Microsoft's compilation behavior, that detects the boolean context and generates != 0 OPCODE.
@v.oddou: That is not a problem at all, whether or not an actual cast to bool occurs (it doesn't). Per the C++ standard on bool conversions: "A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true." (emphasis added) C++ follows C's behavior; most tests care about 0/non-zero, NULL/non-NULL, not true or false.
|
14

C++11 has a new literal keyword nullptr. It's better than 0 or NULL for things like this because there's no chance it will be used as an int in overload resolution.

if ( nullptr == p ) 

Or of course you can just use a pointer in a bool context:

if ( !p ) 

Comments

1

If you have the same problem in 2019, You are using an older initialization style for unassigned pointers. After 2014 August 14 the C++ 11 released. The compiler you are using is probably following the C++11 standard.

You can just replace 0/NULL with nullptr

Modern C++ offers nullptr as a universal null pointer. You should use that instead of 0 || NULL because there are some obscure machines that have things like signed pointers where a null pointer is actually something like -1 rather than 0.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.