Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between NULL and __null in C++?

Tags:

c++

null

keyword

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++?

like image 843
Pika Supports Ukraine Avatar asked Dec 28 '18 19:12

Pika Supports Ukraine


People also ask

What is the difference between NULL and NUL in C?

The C NUL is a single character that compares equal to 0. The C NULL is a special reserved pointer value that does not point to any valid data object. The SQL null value is a special value that is distinct from all non-null values and denotes the absence of a (non-null) value.

What is the difference between null and void in programming?

Void refers to the type. Basically the type of data that it points to is unknown. Null refers to the value. It's essentially a pointer to nothing, and is invalid to use.

What is the difference between void and NULL in C++?

void* is just a pointer to an undefined type. A void* can be set to any memory location. A NULL pointer is a any pointer which is set to NULL (0). So yes, they are different, because a void pointer is a datatype, and a NULL pointer refers to any pointer which is set to NULL.

IS NULL same as 0?

The answer to that is rather simple: a NULL means that there is no value, we're looking at a blank/empty cell, and 0 means the value itself is 0. Considering there is a difference between NULL and 0, the way Tableau treats these two values therefore is different as well.


2 Answers

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

like image 131
ShadowRanger Avatar answered Oct 09 '22 22:10

ShadowRanger


NULL has been overtaken from C into C++ and - prior to C++11 - adopted its C meaning:

until C++11: The macro NULL is an implementation-defined null pointer constant, which may be an integral constant expression rvalue of integer type that evaluates to zero.

C++11 then introduced a dedicated null pointer literal nullptr of type std::nullptr_t. But - probably for backward compatibility - the macro NULL was not removed; its definition was just a bit relaxed in that compilers may now define it either as integral or as pointer type:

C++11 onwards: an integer literal with value zero, or a prvalue of type std::nullptr_t

If you use NULL, then you get implementation-defined behaviour in overload resolution. Consider, for example, the following code with a compiler that uses the integral-version of NULL-macro. Then a call using NULL as parameter passed to a function may lead to ambiguities:

struct SomeOverload {      SomeOverload(int x) {         cout << "taking int param: " << x << endl;     }     SomeOverload(void* x) {         cout << "taking void* param: " << x << endl;     } };  int main() {      int someVal = 10;      SomeOverload a(0);     SomeOverload b(&someVal);      // SomeOverload c(NULL);  // Call to constructor is ambiuous     SomeOverload d(nullptr); } 

So it is recommended to use nullptr where ever you want to express pointer type.

And don't use __null, as this is a compiler-specific, non-portable constant; nullptr, in contrast, is perfectly portable.

like image 27
Stephan Lechner Avatar answered Oct 09 '22 22:10

Stephan Lechner