Nullptr in C++
What is Nullptr in C++?
A null pointer value is represented by the term nullptr. Use a null pointer value to indicate that a native pointer type, inner pointer, or object handle does not point to an object.
We use nullptr with managed or native code. For native and managed null pointer values, the compiler emits instructions that are suitable but distinct. See nullptr for details on how to use the C++ version of this keyword that is ISO compliant.
The __nullptr keyword, which is unique to Microsoft and only used in native code, has the same semantics as nullptr. The compiler cannot tell managed from native null pointer values when native C/C++ code uses nullptr and is built with the /clr compiler option. To make your meaning clear to the compiler, use nullptr to define a managed value or __nullptr to describe a native value.
The C# and Visual Basic equivalents of the keyword nullptr are null and Nothing, respectively.
Use of Nullptr
The keyword nullptr is permitted whenever a handle, native pointer, or function parameter may be used.
The nullptr keyword is not recognised for use with the following:
- sizeof
- typeid
- throw (Object)nullptr; will also work, but throw nullptr
The following pointer types can be initialised with the nullptr keyword:
- local pointer
- Windows Runtime manage
- controlled handle
- controlled internal pointer
Before using a pointer or handle reference, the nullptr keyword can be used to check if it is null. Function calls should be properly interpreted by languages that use null pointer values for error checking. Only nullptr may be used to initialise a handle to zero. A boxed Int32 and a cast to Object are created when constant 0 is assigned to an object handle.
Example of a nullptr keyword
The following line of code illustrates how the nullptr keyword can be used at anyplace a handle, native pointer, or function parameter. Additionally, the example shows how the nullptr keyword may be used to validate a reference before using it.
Example 1:
// C++ program to display the NULL problem
#include <bits/stdC++.h>
using namespace std ;
// an integer-argument function
void fun(int N) {
cout<< "funcion( int ) " ;
return ;
}
// function with a char pointer parameter that is overloaded
void fun(char* s) {
cout<< "function ( char * )" ;
return ;
}
int main()
{
// Although calling fun(char *) would have been ideal,
// doing so results in a compiler error.
fun(NULL) ;
}
Output:
What is wrong with the program?
Normally, NULL is defined as (void *)0, and it is permissible to convert NULL to integral types. As a result, the function call fun(NULL) is unclear.
// This program compiles (may produce warning)
#include<stdio.h>
int main()
{
int a = NULL;
return 0;
}
How does nullptr handle this issue?
If NULL is swapped out for nullptr in the above program, the output reads "fun(char *)".
The keyword nullptr can be used whenever NULL is anticipated. Similar to NULL, Nullptr is implicitly convertible and comparable to any pointer type. It is not inherently convertible or equivalent to integral types, in contrast to NULL.
Example 2:
// The compilation of this program fails.
#include<stdio.h>
int main()
{
int a = nullptr;
}
Output:
As an aside, nullptr can be converted to Boolean value.
Example 3:
// This program compiles
#include<iostream>
using namespace std;
int main()
{
int *pointer = nullptr;
// Below line compiles
if (pointer) {
cout<< "true";
}
else {
cout<< "false";
}
return 0;
}
Output:
When comparing two simple pointers, there are some unspecified factors, but when comparing two values of type nullptr t, it is specified that comparisons by = and >= return true and comparisons by and > returns false, and comparisons by == and!= return true or false depending on whether the value is null or not.
Example 4:
// Program in C++ that displays comparisons with nullptr
#include <bits/stdC++.h>
using namespace std;
// Driver program to test nullptr behaviour
int main()
{
// establishing two variables of type nullptr t,
// so their values are nullptr
nullptr_t nop1, nop2;
// Comparisons between = and >= always return true.
if (nop1 >= nop2)
cout<< "we can compare it " <<endl;
else
cout<< "we can not compare it " <<endl;
// Initialize a pointer with value equal to nop1
char *a = nop1;
// equivalent to a = nullptr
// (a = NULL is also acceptable.)
if (a == nullptr)
cout<< "a is null" <<endl;
else
cout<< "a is not null" <<endl;
return 0;
}
Output: