C++ Memory Management
In this page:
data_type stack_variable = value; // stack: freed automatically
data_type *heap_pointer = new data_type(value); // heap: you free it
delete heap_pointer;
What is Memory Management?
Memory management in C++ is the process of allocating space for data as a program needs it and releasing that space when it's no longer needed, done manually with new/delete or automatically through RAII-based smart pointers.
Example: What is Memory Management?
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Allocate an object on the heap with new
int *ptr = new int(5);
// Print to the console with cout
std::cout << *ptr << std::endl;
// Free the heap memory with delete
delete ptr;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Stack vs Heap Memory
C++ programs use two main regions of memory: the stack, which automatically manages local variables tied to function calls, and the heap, a pool of memory that must be explicitly allocated and freed (or managed by a smart pointer).
Example: Stack vs Heap Memory
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare stackVar and set it to 10
int stackVar = 10;
// Allocate an object on the heap with new
int *heapVar = new int(20);
// Print to the console with cout
std::cout << stackVar << " " << *heapVar << std::endl;
// Free the heap memory with delete
delete heapVar;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Static vs Dynamic Allocation
Static allocation, like a regular array declaration, fixes memory size at compile time, while dynamic allocation, using new, lets a program decide how much memory it needs while it's actually running.
Example: Static vs Dynamic Allocation
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int staticArr[3] = {1, 2, 3};
// Declare size and set it to 3
int size = 3;
// Allocate an object on the heap with new
int *dynamicArr = new int[size];
dynamicArr[0] = 5;
// Print to the console with cout
std::cout << staticArr[0] << " " << dynamicArr[0] << std::endl;
// Free the heap memory with delete
delete[] dynamicArr;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
The Allocate-Use-Free Lifecycle
Every block of dynamically allocated memory follows a lifecycle: it's allocated with new, used through a pointer, and then released exactly once with delete (or delete[] for arrays) when it's no longer needed.
Example: The Allocate-Use-Free Lifecycle
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Allocate an object on the heap with new
int *ptr = new int(5);
// Print to the console with cout
std::cout << *ptr << std::endl;
// Free the heap memory with delete
delete ptr;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Modern C++ Prefers Smart Pointers
Modern C++ encourages using smart pointers like unique_ptr and shared_ptr instead of raw new/delete, since they automatically release their memory when they go out of scope, preventing leaks and use-after-free bugs.
Example: Modern C++ Prefers Smart Pointers
// Include std::cout and std::cin
#include <iostream>
// Include smart pointers
#include <memory>
// Program execution starts in main()
int main() {
// Use a unique_ptr: a smart pointer that owns the object and frees it automatically
std::unique_ptr<int> ptr = std::make_unique<int>(42);
// Print to the console with cout
std::cout << *ptr << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting to free memory allocated with
new, which causes a leak. - Using memory after it has been freed, which is undefined behavior.
- Returning the address of a local (stack) variable from a function.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: