. Advertisement .
..3..
. Advertisement .
..4..
Unlike higher-end programming languages, you must handle manual memory management in C++, including dereferencing.
This approach can boost performance. But when a null pointer is dereferenced without your knowledge, it will crash your program.
Let’s learn how to check if a pointer is null in C++ to avoid this error.
Check If A Pointer Is Null In C++
Using Implicit Conversions To Boolean
By performing implicit conversions, you can check if a pointer is null in C++. According to the C++11 standard, pointers can be converted to bool type. Null pointers return false, while non-null pointers are converted to true.
The easiest way to take advantage of this rule is using an if statement like this example:
#include <stdio.h>
#include <cstdlib>
#include <time.h>
const int LEN = 3;
int main () {
int var[LEN] = {22, 28, 12};
int *ptr[LEN];
srand (time(NULL));
int random = rand() % LEN;
printf("Random order: %d \n", random + 1);
for (int i = 0; i < LEN; i++) {
if (i == random) {
ptr[i] = nullptr;
} else {
ptr[i] = &var[i];
}
}
for (int i = 0; i < LEN; i++) {
if (ptr[i]) {
printf("The value referred to by this pointer: %d \n", *ptr[i]);
} else {
printf("ERROR: This is a null pointer.\n");
}
}
return 0;
}
Result:
Random order: 2
The value referred to by this pointer: 22
ERROR: This is a null pointer.
The value referred to by this pointer: 12
In this snippet, we create an array of five integers and initialize an array of pointers of the same size and type. These pointers can be used to refer to the elements in the first array.
The srand() and rand() functions help us pick a random number from 1 to 3. During the first “for” loop, the program runs through both arrays. If the order matches the random number we have, it assigns a null constant to the corresponding pointer. We use the nullptr value in this snippet.
The second “for” loop iterates over the array of pointers and prints the actual value each of them points to. When the program runs into a null pointer, it produces a warning.
Checking For Null Pointer Constants
While implicit conversions are the most convenient way to check if a pointer is null in C++, we also want to represent another method. It checks for values assigned to null pointers: 0, NULL, and nullptr.
C++ inherits the macro NULL from C as a way to present a null pointer value. It is still commonly used even though 0 is typically preferred over it. The C++11 standard introduces the nullptr keyword as the official explicit null pointer constant.
You can rewrite the previous example as follows.
#include <stdio.h>
#include <cstdlib>
#include <time.h>
const int LEN = 3;
int main () {
int var[LEN] = {22, 28, 12}
int *ptr[LEN];
srand (time(NULL));
int random = rand() % LEN;
printf("Random order: %d \n", random + 1);
for (int i = 0; i < LEN; i++) {
if (i == random) {
ptr[i] = nullptr;
} else {
ptr[i] = &var[i];
}
}
for (int i = 0; i < LEN; i++) {
if (ptr[i] == NULL || ptr[i] == 0 || ptr[i] == nullptr) {
printf("ERROR: This is a null pointer.\n");
} else {
printf("The value referred to by this pointer: %d \n", *ptr[i]);
}
}
return 0;
}
Output:
Random order: 1
ERROR: This is a null pointer.
The value referred to by this pointer: 28
The value referred to by this pointer: 12
Conclusion
Knowing how to check if a pointer is null in C++ is extremely important due to its sensitive nature. Make sure you understand the methods above and their underlying knowledge, so your programs won’t run into avoidable issues.
Leave a comment