47

I like to use nullptr instead of NULL. Now I call a C function (from libjansson in this case).

NULL in C is implementation defined.

For nullptr I found that "A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero".

So the safest thing to do:

auto string_obj=json_object_get(m_handle,name);
if(string_obj!=NULL)
    {
    auto string=json_string_value(string_obj);
    if(string!=NULL)
        {return string;}
    }
return nullptr;

Do I really need that or can I do it simpler:

auto string_obj=json_object_get(m_handle,name);
if(string_obj!=nullptr)
    {
    return json_string_value(string_obj); //Assume there is no difference between C NULL and C++11 nullptr
    }
return nullptr;
user877329
  • 6,717
  • 8
  • 46
  • 88
  • 1
    Chose the language. The language then tells you what to use. You cannot mix-n-match – Ed Heal Apr 07 '16 at 18:35
  • 8
    @EdHeal: This is an interoperability question. – user877329 Apr 07 '16 at 18:36
  • For C functions that you are calling from C++ you use NULL in there arguments as appropriate. Otherwise use nullptr. If moving from C++ to C do `v == nullptr ? NULL : v` and vice versa – Ed Heal Apr 07 '16 at 18:41
  • 3
    @EdHeal Why would you do that? Assigning `nullptr` to a pointer in C++ does the same thing as assigning `NULL` (or heck `0`) does as far as C++ is concerned: `X* ptr = NULL;`, `X* ptr=nullptr;` and `X* ptr=0;` *do the same thing in C++*. Similarly, `ptr == NULL` and `ptr == nullptr` and `ptr==0` and `!ptr` are all guaranteed to return the same value in C++ code. – Yakk - Adam Nevraumont Apr 07 '16 at 19:11
  • I thought that NULL does not have to be zero. Just needs to compare to zero. Therefore nullptr may not equal NULL. http://stackoverflow.com/questions/9894013/is-null-always-zero-in-c – Ed Heal Apr 07 '16 at 19:14
  • `int c = NULL;` will compile. `int c = nullptr;` will not. `nullptr` was designed so that it cannot be assigned to an `int`. There is a difference. – Neil Roy Feb 26 '18 at 16:23
  • `nullptr` is not exactly the same as the "a nullpointer constant" that you "found somewhere, but I don't tell you where" (it's the C++ standard by the way, and an old version of that.) – Roland Illig Mar 13 '20 at 07:17

1 Answers1

89

In C++11 and beyond, a pointer that is ==NULL will also ==nullptr and vice versa.

Uses of NULL other than comparing with a pointer (like using it to represent the nul byte at the end of a string) won't work with nullptr.

In some cases, NULL is #define NULL 0, as the integer constant 0 is special-cased in C and C++ when you compare it with pointers. This non-type type information causes some problems in both C and C++, so in C++ they decided to create a special type and value that does the same thing in the "proper" use cases, and reliably fails to compile in most of the "improper" use cases.

Insofar as your C++ implementation is compatible with the C implementation you are interoping with (very rare for this not to be true), everything should work.


To be very clear, if ptr is any kind of pointer, then the following expressions are equivalent in C++:

ptr == nullptr
ptr == NULL
ptr == 0
!ptr

As are the following:

ptr = nullptr
ptr = NULL
ptr = 0

and if X is some type, so are the following statements:

X* ptr = nullptr;
X* ptr = NULL;
X* ptr = 0;

nullptr differs when you pass it to a template function that deduces type (NULL or 0 become an int unless passed to an argument expecting a pointer, while nullptr remains a nullptr_t), and when used in some contexts where nullptr won't compile (like char c = NULL;) (note, not char* c=NULL;)

Finally, literally:

NULL == nullptr

is true.

The NULL constant gets promoted to a pointer type, and as a pointer it is a null pointer, which then compares equal to nullptr.


Despite all this, it isn't always true that:

 foo(NULL)

and

 foo(nullptr)

do the same thing.

void bar(int) { std::cout << "int\n"; }
void bar(void*) { std::cout << "void*\n"; }
template<class T>
void foo(T t) { bar(t); }
foo(NULL);
foo(nullptr);

this prints int for NULL and void* for nullptr.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524