What You’ll Learn
By the end of this post, you will:
- Understand how memory works in a C++ program
- Learn the difference between stack and heap memory
- Work with pointers and references
- Dynamically allocate and deallocate memory using
new
anddelete
- Avoid common pitfalls like memory leaks and dangling pointers
Stack vs Heap Memory
C++ uses two main types of memory:
Memory Type | Managed By | Lifetime | Use Case |
---|---|---|---|
Stack | Compiler | Automatically freed | Local variables, function calls |
Heap | Manually managed | Remains until deleted | Dynamic allocation (new ) |
Example:
int x = 5; // Stored on the stack
int* p = new int(10); // Allocated on the heap
x
is destroyed when the function ends.p
points to heap memory, which must be deleted manually.
What is a Pointer?
A pointer is a variable that stores the memory address of another variable.
Declaring a Pointer:
int x = 42;
int* ptr = &x;
&x
means “address of x”*ptr
means “value at the address stored in ptr”
Accessing Values:
cout << "x = " << x << endl;
cout << "Address of x = " << &x << endl;
cout << "ptr points to = " << ptr << endl;
cout << "Value at ptr = " << *ptr << endl
;
Changing Values via Pointers
int x = 5;
int* p = &x;
*p = 10; // Changes x to 10
cout << x << endl; // Output: 10
What is a Reference?
A reference is an alias for another variable. Once created, it cannot be changed to refer to something else.
Syntax:
int a = 5;
int& ref = a;
ref = 10; // Changes a to 10
Unlike pointers:
- References must be initialized when declared
- No need to dereference (
*
) or use address-of (&
) once bound
Dynamic Memory Allocation
You can allocate memory at runtime using new
.
Example: Single Variable
int* p = new int;
*p = 25;
cout << *p << endl;
delete p; // Free memory
Example: Dynamic Array
int* arr = new int[5]; // allocate array of size 5
for (int i = 0; i < 5; i++) {
arr[i] = i * 2;
}
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
delete[] arr; // Always use delete[] for arrays
Memory Leaks
If you forget to delete
memory, it stays allocated until the program ends. This is a memory leak.
int* leak = new int(100);
// no delete -> memory leak
To prevent leaks, always pair new
with delete
.
Dangling Pointers
A dangling pointer points to memory that has been deallocated.
int* p = new int(50);
delete p;
*p = 10; // undefined behavior
After delete
, set the pointer to nullptr
:
delete p;
p = nullptr;
nullptr
in C++
Modern C++ uses nullptr
(instead of NULL
) to represent an empty or invalid pointer.
int* p = nullptr;
if (p == nullptr) {
cout << "Pointer is null" << endl;
}
Summary Table
Concept | Description |
---|---|
Stack Memory | Automatically managed, for local variables |
Heap Memory | Manually managed using new and delete |
Pointer | Stores memory address, can modify values indirectly |
Reference | Alias for a variable, simpler than pointer |
Memory Leak | Forgetting to delete heap memory |
Dangling Pointer | Using a pointer after memory is deleted |
nullptr | Safer way to check if pointer is empty |
Quiz: Dynamic Memory Allocation
Mini Exercises
- Write a program that:
- Declares an
int
variable and a pointer to it - Changes the variable’s value using the pointer
- Declares an
- Dynamically allocate an array of 5 integers, store values, print them, and free memory.
- Use a reference to swap two values (without using pointers).
- Create a function that allocates an array using
new
and returns the pointer.
Common Mistakes to Avoid
- Forgetting to
delete
dynamically allocated memory - Using a pointer after
delete
(dangling pointer) - Confusing
*
in declaration (int* p
) vs. dereference (*p = 10
) - Trying to rebind a reference after it’s been assigned
Summary
In this post, you learned:
- How C++ uses stack and heap memory
- The basics of pointers and references
- How to allocate and free memory with
new
anddelete
- How to avoid memory-related bugs like leaks and dangling pointers
What’s Next?
In the next post, we’ll explore Recursion in C++ — functions that call themselves, and how they solve problems like factorials, Fibonacci, and the Tower of Hanoi.