Null Pointer in C++
Null Pointer Program in C++.
BeginnerMemory Management ProgramsExample 6 of 11
null-pointer.cpp
1#include <iostream>2using namespace std;34int main() {5 int* ptr = nullptr; // C++11 way (preferred)6 // int* ptr = NULL; // C way7 // int* ptr = 0; // Alternative89 cout << "Pointer value: " << ptr << endl;1011 // Check if pointer is null12 if (ptr == nullptr) {13 cout << "Pointer is null (not pointing to anything)" << endl;14 }1516 // Safe to check before dereferencing17 if (ptr != nullptr) {18 cout << "Value: " << *ptr << endl;19 } else {20 cout << "Cannot dereference null pointer!" << endl;21 }2223 // Allocate memory24 ptr = new int(42);2526 if (ptr != nullptr) {27 cout << "\nAfter allocation:" << endl;28 cout << "Pointer value: " << ptr << endl;29 cout << "Value pointed: " << *ptr << endl;30 }3132 // Deallocate and set to null33 delete ptr;34 ptr = nullptr; // Good practice: set to null after delete3536 if (ptr == nullptr) {37 cout << "\nPointer set to null after deletion" << endl;38 }3940 return 0;41}
Output
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
What's going on
The program demonstrates null pointer usage:
Initializing pointers to nullptr
Checking if pointer is null before dereferencing
Setting pointer to null after deletion
Safe pointer handling practices
Null pointers prevent undefined behavior and crashes.
Header File Used
This header provides:
cout for displaying output
cin for taking input from the user
1#include <iostream>
Understanding Null Pointers
Null Pointer Concept
:
Pointer that doesn't point to any valid memory
Indicates "no object" or "not initialized"
Safe to check but not to dereference
Essential for safe pointer handling
C++11 Way
:
int* ptr = nullptr; // Preferred
Legacy Ways
:
int* ptr = NULL; // C way (macro)
int* ptr = 0; // Alternative (not recommended)
Initializing to Null
Safe Initialization
:
int* ptr = nullptr; // Initialize to null
Why?
Prevents uninitialized pointer
Can safely check if (ptr == nullptr)
Prevents accidental dereferencing
Good programming practice
Checking for Null
Before Dereferencing
:
if (ptr != nullptr) {
} else {
cout << "Pointer is null!" << endl;
}
1cout << *ptr << endl; // Safe to dereference
How it works
:
Always check before dereferencing
Prevents crashes from null pointer dereference
Essential safety check
Setting to Null After Deletion
Good Practice
:
delete ptr;
ptr = nullptr; // Set to null after deletion
Why?
Prevents dangling pointer
Can check if pointer was deleted
Prevents double deletion
Makes code safer
When to Use Null Pointers
Best For
:
Pointer initialization
Error handling (function returns null on error)
End-of-list markers (linked lists)
Optional parameters
Conditional object creation
Example Scenarios
:
Function returns null if allocation fails
Linked list: null indicates end
Optional function parameters
Conditional object pointers
Null Pointer Dereference
Danger
:
int* ptr = nullptr;
*ptr = 10; // CRASH! Undefined behavior
Why Crashes?
Tries to access invalid memory
Operating system prevents access
Program crashes or undefined behavior
Always check before dereferencing
Important Considerations
nullptr vs NULL vs 0
:
nullptr: C++11, type-safe, preferred
NULL: C macro, less type-safe
0: Works but not recommended
Type Safety
:
nullptr is type-safe
Cannot be confused with integer 0
Better compiler error messages
Best Practices
:
Always initialize pointers
Check for null before dereferencing
Set to null after deletion
Use nullptr (not NULL or 0)
return 0;
This ends the program successfully.