Null Pointer
Null Pointer Program in C++
C++ Null Pointer Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int main() {
int* ptr = nullptr; // C++11 way (preferred)
// int* ptr = NULL; // C way
// int* ptr = 0; // Alternative
cout << "Pointer value: " << ptr << endl;
// Check if pointer is null
if (ptr == nullptr) {
cout << "Pointer is null (not pointing to anything)" << endl;
}
// Safe to check before dereferencing
if (ptr != nullptr) {
cout << "Value: " << *ptr << endl;
} else {
cout << "Cannot dereference null pointer!" << endl;
}
// Allocate memory
ptr = new int(42);
if (ptr != nullptr) {
cout << "\nAfter allocation:" << endl;
cout << "Pointer value: " << ptr << endl;
cout << "Value pointed: " << *ptr << endl;
}
// Deallocate and set to null
delete ptr;
ptr = nullptr; // Good practice: set to null after delete
if (ptr == nullptr) {
cout << "\nPointer set to null after deletion" << endl;
}
return 0;
}Pointer value: 0 Pointer is null (not pointing to anything) Cannot dereference null pointer! After allocation: Pointer value: 0x7f8a5b402670 Value pointed: 42 Pointer set to null after deletion
Understanding Null Pointer
This program teaches you how to use Null Pointers in C++. A null pointer doesn't point to any valid memory location. It's essential for safe pointer handling, preventing crashes, and indicating that a pointer doesn't currently point to anything.
---
1. What This Program Does
The program demonstrates null pointer usage:
Null pointers prevent undefined behavior and crashes.
---
2. Header File Used
This header provides:
---
#include <iostream>3. Understanding Null Pointers
Null Pointer Concept
:
C++11 Way
:
int* ptr = nullptr; // Preferred
Legacy Ways
:
int* ptr = NULL; // C way (macro)
int* ptr = 0; // Alternative (not recommended)
---
4. Initializing to Null
Safe Initialization
:
int* ptr = nullptr; // Initialize to null
Why?
---
5. Checking for Null
Before Dereferencing
:
if (ptr != nullptr) {
} else {
cout << "Pointer is null!" << endl;
}
cout << *ptr << endl; // Safe to dereferenceHow it works
:
---
6. Setting to Null After Deletion
Good Practice
:
delete ptr;
ptr = nullptr; // Set to null after deletion
Why?
---
7. When to Use Null Pointers
Best For
:
Example Scenarios
:
---
8. Null Pointer Dereference
Danger
:
int* ptr = nullptr;
*ptr = 10; // CRASH! Undefined behavior
Why Crashes?
---
9. Important Considerations
nullptr vs NULL vs 0
:
Type Safety
:
Best Practices
:
---
10. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning safe pointer practices, understanding error handling, and preparing for robust memory management in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Null Pointer
This C++ program is part of the "Memory Management Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.