C++ Dynamic Memory Management | new, delete, RAII, Memory Leaks and malloc vs new


This complete tutorial on C++ Dynamic Memory Management explains how to allocate and deallocate memory at runtime using new, delete, new[], and delete[]. It also covers malloc vs new, memory leaks, and the RAII concept for safe memory handling. Following best practices, it helps learners write efficient and safe C++ programs.

Dynamic Memory Management – Complete Tutorial

1. What is Dynamic Memory Management?

Dynamic memory management allows allocating memory at runtime, instead of fixed memory at compile time.

  1. Useful for objects whose size is unknown during compilation
  2. Memory must be manually managed to avoid leaks

2. new and delete

  1. new allocates memory for a single object
  2. delete frees memory allocated by new

Example:


#include <iostream>
using namespace std;

int main() {
int *ptr = new int; // dynamically allocate an integer
*ptr = 10;
cout << *ptr << endl;

delete ptr; // free memory
ptr = nullptr; // good practice
}

3. new[] and delete[]

Used for dynamic arrays.

Example:


int *arr = new int[5]; // allocate array of 5 integers
for(int i=0; i<5; i++) arr[i] = i+1;

for(int i=0; i<5; i++) cout << arr[i] << " ";
delete[] arr; // free array memory
arr = nullptr;

Key Points:

  1. Always use delete[] for arrays allocated with new[]
  2. Avoid mismatched delete/delete[]

4. malloc vs new

Featuremallocnew
Header<cstdlib>none
Type SafetyNoYes
ConstructorNot calledCalled
Returnsvoid*Object pointer
Free Memoryfree()delete

Best Practice: Prefer new and delete in C++ because they handle constructors/destructors automatically.

5. Memory Leaks

Memory leaks occur when dynamically allocated memory is not freed.

Example:


int *ptr = new int(10);
// no delete here → memory leak

How to Avoid:

  1. Always pair new with delete and new[] with delete[]
  2. Use smart pointers (std::unique_ptr, std::shared_ptr)
  3. Set pointers to nullptr after deletion

6. RAII Concept (Resource Acquisition Is Initialization)

RAII ensures resources are automatically released when objects go out of scope.

Example with smart pointer:


#include <iostream>
#include <memory>
using namespace std;

int main() {
unique_ptr<int> p = make_unique<int>(10);
cout << *p << endl;
// memory automatically freed when p goes out of scope
}

Key Points:

  1. Prevents memory leaks
  2. Ensures automatic cleanup of resources
  3. Use smart pointers in modern C++

Best Practices

  1. Always delete dynamically allocated memory
  2. Use nullptr after deletion
  3. Prefer smart pointers for automatic memory management
  4. Avoid excessive dynamic allocation if possible
  5. Follow RAII principles for robust resource management

Common Mistakes

  1. Forgetting to free memory → memory leak
  2. Using memory after deletion → dangling pointer
  3. Mixing malloc with delete or new with free
  4. Not handling exceptions that may bypass delete

Summary

In this chapter, you learned about C++ Dynamic Memory Management, including:

  1. new and delete for single objects
  2. new[] and delete[] for arrays
  3. Difference between malloc and new
  4. Memory leaks and how to avoid them
  5. RAII and smart pointers for safe memory handling

Dynamic memory management is essential for efficient C++ programming, especially for large programs and real-world applications.