A NULL pointer in C++ represents a pointer that does not refer to any valid memory address.
- It indicates that the pointer is intentionally set to point to nothing and holds the value NULL (or nullptr in C++11 and later).
- It is commonly used during variable declaration to show that a pointer is not yet associated with any memory, and it is also returned by many built-in functions to indicate a failure condition.
- Attempting to dereference a NULL pointer results in undefined behavior and may cause the program to crash.
Syntax and Example
We can create a NULL pointer of any type by simply assigning the value NULL to the pointer as shown:
int* ptrName = NULL; // before C++11
int* ptrName = nullptr
int* ptrName = 0; // by assigning the value 0
A null pointer is represented by the value 0 or by using the keyword NULL. With the new versions of C++ like C++11 and later, we can use "nullptr" to indicate a null pointer.
#include <iostream>
using namespace std;
int main(){
int* ptr = nullptr;
if (ptr == nullptr) {
cout << "Pointer is currently null." << endl;
}
else {
cout << "Pointer is not null." << endl;
}
// *ptr = 10; (to avoid runtime error)
// Assigning a valid memory address to the pointer
int value = 5;
ptr = &value;
// Checking if the pointer is null after assigning a
// valid address
if (ptr == nullptr) {
cout << "Pointer is currently null." << endl;
}
else {
cout << "Pointer is not null." << endl;
cout << "Value at the memory location pointed to "
"by the pointer: "
<< *ptr << endl;
}
return 0;
}
Output
Pointer is currently null. Pointer is not null. Value at the memory location pointed to by the pointer: 5
Explanation:
- First, the pointer is set to a NULL value.
- The program checks whether the pointer is NULL before dereferencing it to avoid runtime errors.
- Then a valid memory address is assigned to the pointer.
- The pointer is checked again before dereferencing.
- Since the pointer is no longer NULL, the else part is executed.
Checking NULL Pointer
We can check whether a pointer is a NULL pointer by using the equality comparison operator.
ptrName == NULL
or
ptrName == nullptr
The above expression will return true if the pointer is a NULL pointer. False otherwise.
Applications of Null Pointer in C++
- Initialization: It is a good practice to Initialize pointers to a null value as it helps avoid undefined behavior by explicitly indicating they are not pointing to valid memory locations.
- Default Values: Null pointers act as default or initial values for pointers when no valid address is assigned to the pointers.
- Error Handling: They are useful in error conditions or to signify the absence of data that enables better handling of exceptional cases.
- Resource Release: To release the resources, like the destructor of a class, or to set pointers to NULL after deletion we can use a null pointer to avoid accidentally using or accessing the released memory.
- Sentinel Values: A null pointer can be used to indicate the end of a data structure or a list like in the linked list last node has a null pointer as the next field.
Issues with NULL
NULL pointer makes it possible to check for pointer errors but it also has its limitations:
- Dereferencing a NULL pointer causes undefined behavior that may lead to runtime errors like segmentation faults.
- We need to check explicitly for NULL pointers before dereferencing it to avoid undefined behavior.