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++?
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.
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.
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.
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.
__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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With