A NULL Pointer in C++ indicates the absence of a valid memory address in C++. It tells that the pointer is not pointing to any valid memory location In other words, it has the value "NULL" (or 'nullptr' since C++11). This is generally done at the time of variable declaration to check whether the pointer points to some valid memory address or not. It is also returned by several inbuilt functions as a failure response.
Trying to dereference a NULL pointer i.e. trying to access the memory it points to leads to some undefined behavior leading to the program crash.
Syntax of Null Pointer in C++
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.
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++
Null Pointer finds its applications in the following scenarios:
- 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.
Example of NULL Pointer in C++
The below example demonstrates the dereferencing and assignment of a null pointer to another value.
C++
// C++ program to demonstrate the dereferencing and
// assignment of null pointer to another value.
#include <iostream>
using namespace std;
int main()
{
int* ptr = nullptr;
// Checking if the pointer is null before dereferencing
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;
}
OutputPointer is currently null.
Pointer is not null.
Value at the memory location pointed to by the pointer: 5
Explanation: In the example given above first the pointer is pointing to a null value. First, we check whether the pointer is pointing to a null value or not before dereferencing it to avoid any kind of runtime error. Then we assign the pointer a valid memory address and then check it before dereferencing it. As the pointer is not pointing to a null value, the else part is executed.
Disadvantages of NULL Pointers in C++
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.
Conclusion
It is important to understand null pointers in C++ to handle pointers safely and prevent unexpected runtime errors. They signify the absence of valid memory addresses and help in error handling and pointer initialization. Proper usage and precautions regarding null pointers are essential in writing error-free C++ code.
Similar Reads
Function Pointer in C++
Prerequisites: Pointers in C++Function in C++ Pointers are symbolic representations of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Iterating over elements in arrays or other data structures is one of the main use of point
4 min read
type_traits::is_null_pointer in C++
The type_traits::is_null_pointer of C++ STL is used to check whether the given type is null_pointer or not. It returns the boolean value either true or false. Below is the syntax for the same: Header File: #include<type_traits> Syntax: template class T struct is_null_pointer; Parameter: The te
2 min read
C++ Pointers
A pointer is a variable that stores the address of another variable. Pointers can be used with any data type, including basic types (e.g., int, char), arrays, and even user-defined types like classes and structures.Create PointerA pointer can be declared in the same way as any other variable but wit
8 min read
SQLite IS NULL
SQLite is a server-less database engine and it is written in c programming language. It is developed by D. Richard Hipp in the year 2000. The main moto for developing the SQLite is to escape from using the complex database engines like MYSQL.etc. It has become one of the most popular database engine
6 min read
is_pointer Template in C++
The std::is_pointer template of C++ STL is used to check whether the given type is pointer or not. It returns a boolean value showing the same. Syntax: template <class T > struct is_pointer; Parameter: This template accepts a single parameter T (Trait class) to check whether T is a pointer or
2 min read
Understanding nullptr in C++
Consider the following C++ program that shows problem with NULL (need of nullptr) CPP // C++ program to demonstrate problem with NULL #include <bits/stdc++.h> using namespace std; // function with integer argument void fun(int N) { cout << "fun(int)"; return;} // Overloaded fun
3 min read
What is a Pointer to a Null pointer
NULL pointer in C At the very high level, we can think of NULL as a null pointer which is used in C for various purposes. Some of the most common use cases for NULL are To initialize a pointer variable when that pointer variable isnât assigned any valid memory address yet. C int* pInt = NULL; To che
2 min read
isfinite() function in C++
The isfinite() function is a builtin function in C++ and is used to determine whether a given value if finite or not. A finite value is a value that is neither infinite nor NAN. If the number is finite then the function returns 1 else returns zero.Syntax: bool isfinite(float x); or, bool isfinite(do
2 min read
C++ Pointer Operators
Prerequisite: Pointers in C++ A pointer variable is a variable that stores the address of another variable or in other a pointer variable points to the variable whose address is stored inside it. Syntax: int *pointer_name; There are mainly two types of Pointer operators mainly used: Address of opera
2 min read
Rank and Nullity
Rank and Nullity are essential concepts in linear algebra, particularly in the context of matrices and linear transformations. They help describe the number of linearly independent vectors and the dimension of the kernel of a linear mapping. In this article, we will learn what Rank and Nullity, the
11 min read