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

C++ Memory Leaks

A memory leak happens when a program borrows memory and never gives it back, like never returning library books. Over time the computer runs out of space.
Syntax
cpp
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?

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

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

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

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

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

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

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 *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;
}

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

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>(5);
	// 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. Reassigning a pointer to a new allocation before deleting the old one, which loses the only reference.
  2. Returning early from a function before reaching the delete.
  3. Using delete on a pointer allocated with new[].
🔒

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.