Memory in C++: Dynamic Memory
& Pointers
Understand how memory works in C++ programs. Learn the difference between stack and heap, master pointers and references, and avoid memory leaks and dangling pointers.
- How memory works in a C++ program
- The difference between stack and heap memory
- How to work with pointers and references
- How to dynamically allocate and deallocate memory
- How to avoid memory leaks and dangling pointers
1Stack vs heap memory
C++ uses two main types of memory: stack and heap. Understanding the difference is crucial for writing efficient and bug-free code.
| Memory Type | Managed By | Lifetime | Use Case |
|---|---|---|---|
| Stack | Compiler | Automatically freed | Local variables, function calls |
| Heap | Manually managed | Remains until deleted | Dynamic allocation (new) |
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.
2What is a pointer?
A pointer is a variable that stores the memory address of another variable. It is one of the most powerful features in C++.
Declaring a pointer:
int x = 42; int* ptr = &x;
&xmeans "address of x"*ptrmeans "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;
3Changing values via pointers
Using a pointer, you can modify the value of the variable it points to.
int x = 5; int* p = &x; *p = 10; // Changes x to 10 cout << x << endl;
4What 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 - References cannot be reassigned to refer to a different variable
5Dynamic memory allocation
You can allocate memory at runtime using new and free it using delete.
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
delete for single variables allocated with new, and delete[] for arrays allocated with new[]. Mixing them causes undefined behavior.
6Memory leaks
A memory leak occurs when you allocate heap memory and forget to deallocate it. The memory remains allocated until the program ends, wasting resources.
int* leak = new int(100); // no delete -> memory leak
new must be paired with a delete. In larger programs, consider using smart pointers (unique_ptr, shared_ptr) to manage memory automatically.
7Dangling pointers
A dangling pointer points to memory that has already been deallocated. Using it leads to undefined behavior.
int* p = new int(50); delete p; *p = 10; // undefined behavior – dangling pointer
How to prevent: After deleting a pointer, set it to nullptr.
delete p; p = nullptr;
8nullptr in C++
Modern C++ uses nullptr (instead of NULL) to represent an empty or invalid pointer. It is type-safe and avoids ambiguity.
int* p = nullptr; if (p == nullptr) { cout << "Pointer is null" << endl; }
9Summary 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 |
10Common mistakes to avoid
new with delete.
nullptr after deletion.
int* p declares a pointer. *p = 10 dereferences it to assign a value. They are different operations.
11Mini exercises
- Declare an int variable and a pointer to it. Change the variable's value using the pointer.
- Dynamically allocate an array of 5 integers, store values, print them, and free the memory.
- Use a reference to swap two values (without using pointers).
- Create a function that allocates an array using
newand returns the pointer.
Hint: For swapping with references, declare void swap(int &a, int &b) and use a temporary variable.
