C++ NULL vs __null

  • A+

I have the following code:

MyType x = NULL; 

NetBeans gave me a suggestion to change it to this:

MyType x = __null; 

I looked it up and found that __null is called a "compiler keyword", which I assumed to mean it's used internally for the compiler. I don't understand why NetBeans suggested to change it to a compiler keyword.

What's the difference between NULL and __null in c++?


__null is a g++ internal thing that serves roughly the same purpose as the standard nullptr added in C++11 (acting consistently as a pointer, never an integer).

NULL is defined as 0, which can be implicitly used as integer, boolean, floating point value or pointer, which is a problem when it comes to overload resolution, when you want to call the function that takes a pointer specifically.

In any event, you shouldn't use __null because it's a g++ implementation detail, so using it guarantees non-portable code. If you can rely on C++11 (surely you can by now?), use nullptr. If not, NULL is your only portable option.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: