← Back to C++ Course | Chapter 14: Memory Management | Lesson 1 of 6

C++ Memory Management

Memory management in C++ is the process of manually allocating memory on the heap with new and releasing it with delete, or using RAII-based smart pointers to automate that lifecycle safely.
Syntax
cpp
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?

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting to free memory allocated with new, which causes a leak.
  2. Using memory after it has been freed, which is undefined behavior.
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.