C++ Memory Leaks
In this page:
data_type *pointer_name = new data_type(value);
// missing delete causes a leak
delete pointer_name;
What is a Memory Leak?
A memory leak occurs when heap memory allocated with new is never released with a matching delete, so the program loses any way to free it even though it's no longer using it.
A long-running program with leaks gradually consumes more and more RAM until performance degrades or the system runs out of memory.
Example: What is a Memory Leak?
#include <iostream>
int main() {
int *ptr = new int(5);
// missing delete ptr; here would leak this memory
delete ptr;
std::cout << "Freed correctly" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reassigning Pointers without Deallocating
If you reassign a pointer to a new address without first calling delete on the old one, the original memory block becomes unreachable -- there's no remaining pointer anywhere in the program that references it, so it can never be freed, even though it's technically still "in use" as far as the OS is concerned.
Example: Reassigning Pointers without Deallocating
// 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);
// Free the heap memory with delete
delete ptr;
// Allocate an object on the heap with new
ptr = new int(10);
// 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
Leaks inside Functions
Allocating memory inside a function with new and storing the address only in a local pointer variable means that address is lost the moment the function returns, unless you explicitly return the pointer or store it somewhere that outlives the function call.
Otherwise that memory leaks silently on every call.
Example: Leaks inside Functions
// Include std::cout and std::cin
#include <iostream>
int *createValue() {
// Allocate an object on the heap with new
int *ptr = new int(42);
// Send ptr back to the caller
return ptr;
}
// Program execution starts in main()
int main() {
int *result = createValue();
// Print to the console with cout
std::cout << *result << std::endl;
// Free the heap memory with delete
delete result;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Dynamic Arrays and Incorrect Delete
Freeing a dynamically allocated array with a plain delete instead of delete[] only destructs and frees the first element correctly -- the rest of the array's memory is never properly released, causing a partial leak that's easy to miss since the program doesn't crash immediately.
Example: Dynamic Arrays and Incorrect Delete
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Allocate an object on the heap with new
int *arr = new int[5];
// Free the heap memory with delete
delete[] arr;
// Print to the console with cout
std::cout << "Array freed correctly" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Best Practices for Prevention
The most reliable way to avoid leaks is to minimize how long you hold raw new/delete pairs, and prefer C++ smart pointers (unique_ptr, shared_ptr) which automatically call delete when they go out of scope -- turning manual bookkeeping into something the compiler enforces for you.
Example: Best Practices for Prevention
// 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>(5);
// 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
- Reassigning a pointer to a new allocation before deleting the old one, which loses the only reference.
- Returning early from a function before reaching the
delete. - Using
deleteon a pointer allocated withnew[].
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: