Module 8: Pointers in C++
A pointer is a variable that stores the memory address of another variable. Pointers are widely used for efficient memory management, dynamic memory allocation, arrays, functions, and advanced programming concepts.
Example:
1int age = 25; 2int* ptr = &age; 3 4cout << *ptr;
Output
125
1. Introduction to Pointers
Pointers hold the memory address of variables instead of storing actual values.
Example
1#include <iostream> 2using namespace std; 3 4int main() 5{ 6 int number = 100; 7 int* ptr = &number; 8 9 cout << ptr; 10 11 return 0; 12}
The output is the memory address of number.
2. Memory
Every variable is stored in the computer's memory at a unique address.
Example:
| Variable | Value | Address (Example) |
|---|---|---|
| number | 100 | 0x61FF08 |
Pointers store these addresses rather than the values themselves.
3. Address Operator (&)
The address operator (&) returns the memory address of a variable.
Example
1int age = 20; 2 3cout << &age;
4. Dereference Operator (*)
The dereference operator (*) accesses the value stored at the pointer's address.
Example
1#include <iostream> 2using namespace std; 3 4int main() 5{ 6 int value = 50; 7 int* ptr = &value; 8 9 cout << *ptr; 10 11 return 0; 12}
Output
150
5. Pointer Arithmetic
Pointers can move through consecutive memory locations.
Example
1#include <iostream> 2using namespace std; 3 4int main() 5{ 6 int arr[] = {10, 20, 30}; 7 8 int* ptr = arr; 9 10 cout << *ptr << endl; 11 12 ptr++; 13 14 cout << *ptr; 15 16 return 0; 17}
Output
110 220
6. Null Pointer
A null pointer does not point to any valid memory location.
Example
1int* ptr = nullptr;
Always initialize unused pointers with nullptr.
7. Void Pointer
A void pointer can store the address of any data type but must be cast before dereferencing.
Example
1#include <iostream> 2using namespace std; 3 4int main() 5{ 6 int value = 100; 7 8 void* ptr = &value; 9 10 cout << *(int*)ptr; 11 12 return 0; 13}
Output
1100
8. Wild Pointer
A wild pointer is an uninitialized pointer. Accessing it causes undefined behavior.
Incorrect Example
1int* ptr; 2 3cout << *ptr;
Always initialize pointers before use.
9. Dangling Pointer
A dangling pointer points to memory that has already been freed or is no longer valid.
Example
1int* ptr = new int(10); 2 3delete ptr; 4 5ptr = nullptr;
Assigning nullptr after delete helps prevent accidental access.
10. Pointers and Arrays
The name of an array acts as a pointer to its first element.
Example
1#include <iostream> 2using namespace std; 3 4int main() 5{ 6 int numbers[] = {5, 10, 15}; 7 8 int* ptr = numbers; 9 10 cout << *(ptr + 1); 11 12 return 0; 13}
Output
110
11. Pointers and Functions
Pointers can be passed to functions to modify original values.
Example
1#include <iostream> 2using namespace std; 3 4void increment(int* value) 5{ 6 (*value)++; 7} 8 9int main() 10{ 11 int number = 10; 12 13 increment(&number); 14 15 cout << number; 16 17 return 0; 18}
Output
111
12. Double Pointer
A double pointer stores the address of another pointer.
Example
1#include <iostream> 2using namespace std; 3 4int main() 5{ 6 int value = 20; 7 8 int* ptr = &value; 9 10 int** dptr = &ptr; 11 12 cout << **dptr; 13 14 return 0; 15}
Output
120
13. Smart Pointer Introduction
Smart pointers automatically manage dynamically allocated memory and help prevent memory leaks.
Common smart pointers:
std::unique_ptrstd::shared_ptrstd::weak_ptr
Example
1#include <iostream> 2#include <memory> 3 4using namespace std; 5 6int main() 7{ 8 unique_ptr<int> ptr = make_unique<int>(100); 9 10 cout << *ptr; 11 12 return 0; 13}
Output
1100
Smart pointers were introduced in C++11 and are recommended over raw pointers for dynamic memory management.
Comparison of Pointer Types
| Pointer Type | Purpose |
|---|---|
| Normal Pointer | Stores a memory address |
| Null Pointer | Points to no valid object |
| Void Pointer | Generic pointer type |
| Wild Pointer | Uninitialized pointer |
| Dangling Pointer | Points to released memory |
| Double Pointer | Stores the address of another pointer |
| Smart Pointer | Automatically manages memory |
Best Practices
- Initialize pointers with
nullptr. - Avoid using uninitialized (wild) pointers.
- Set pointers to
nullptrafterdelete. - Prefer smart pointers over raw pointers for dynamic memory.
- Check pointers before dereferencing them.
- Avoid unnecessary pointer arithmetic.
Practice Problems
- Create a pointer to an integer and print its value.
- Swap two numbers using pointers.
- Traverse an array using pointer arithmetic.
- Pass a pointer to a function and modify a variable.
- Demonstrate a double pointer.
- Allocate and free memory using
newanddelete. - Create a simple example using
std::unique_ptr.
Interview Questions
- What is a pointer in C++?
- What is the difference between
&and*? - What is pointer arithmetic?
- What is a null pointer?
- What is a void pointer?
- What is a wild pointer?
- What is a dangling pointer?
- How are pointers used with arrays?
- What is a double pointer?
- Why are smart pointers preferred over raw pointers?
Summary
In this module, you learned:
- What pointers are and how they store memory addresses.
- How to use the address (
&) and dereference (*) operators. - How pointer arithmetic works with arrays.
- The differences between null, void, wild, and dangling pointers.
- How pointers interact with arrays and functions.
- How double pointers work.
- Why smart pointers are recommended for safer memory management.
Next Module: Module 9: Object-Oriented Programming (OOP) — Classes, Objects, Constructors, Destructors, Encapsulation, Inheritance, Polymorphism, Abstraction, Virtual Functions, Friend Functions, and Operator Overloading.