Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔵 Core C++  ·  Lesson 27

Dynamic Memory in C++ (new, delete)

In this lesson: Allocate memory with new and free with delete in C++ — arrays, memory leaks and best practices. In Hindi & English.

Why dynamic memory?

Normal variables have a size fixed when you write the code. But sometimes you only learn how much memory you need while the program runs — for example an array sized by user input. Dynamic memory lets you request memory at run time from the heap.

Allocating with new

new reserves memory on the heap and returns a pointer to it:

C++
int *p = new int;      // one int on the heap
*p = 42;
cout << *p << "\n";     // 42

Freeing with delete

Heap memory is not freed automatically. You must return it with delete when done — every new needs a matching delete:

C++
int *p = new int(42);
cout << *p << "\n";
delete p;              // free it
p = nullptr;           // avoid a dangling pointer
Output:
42

Dynamic arrays

Use new type[size] to make an array whose size is decided at run time — and free it with delete[]:

C++
int n = 5;
int *a = new int[n];        // array of n ints
for (int i = 0; i < n; i++) a[i] = i * 10;
for (int i = 0; i < n; i++) cout << a[i] << " ";
delete[] a;                 // note the [] for arrays
Output:
0 10 20 30 40
💡 Match the forms

new pairs with delete; new[] pairs with delete[]. Mixing them is undefined behaviour.

Memory leaks & dangling pointers

  • A memory leak is forgetting to delete, so the memory stays reserved forever.
  • A dangling pointer is a pointer still holding an address after that memory was freed — using it is undefined behaviour.

Setting a pointer to nullptr right after delete avoids accidental dangling use.

Smart pointers

Modern C++ prefers smart pointers — unique_ptr and shared_ptr from <memory> — which free their memory automatically when they go out of scope, so you cannot forget to delete:

C++
#include <memory>
auto p = make_unique<int>(42);   // frees itself automatically
cout << *p;

See the smart pointers lesson for details.

Common mistakes

  • Forgetting delete — a memory leak.
  • Calling delete twice on the same pointer (double free).
  • Using delete instead of delete[] for arrays.
  • Using a pointer after it was deleted (dangling pointer).
🏋️ Practice

Ask the user for a size, dynamically allocate an int array of that size, fill it with squares (1, 4, 9, ...), print it, then free it correctly with delete[]. Then rewrite it using make_unique.

Summary

  • Dynamic memory is allocated at run time on the heap with new.
  • Every new needs a matching delete; new[] needs delete[].
  • Forgetting to free causes a memory leak; using freed memory is a dangling pointer.
  • Set pointers to nullptr after deleting.
  • Prefer smart pointers, which free memory automatically.

Frequently Asked Questions

What is dynamic memory in C++?
Dynamic memory is memory allocated at run time from a region called the heap, using new, rather than fixed at compile time. It lets a program request exactly as much memory as it needs while running — for example an array whose size is only known from user input.
What is the difference between new and delete in C++?
new allocates memory on the heap and returns a pointer to it, while delete frees memory that new allocated. Every new must be matched by exactly one delete (or delete[] for arrays) to avoid leaking memory.
What is a memory leak in C++?
A memory leak happens when you allocate memory with new but never delete it, so the memory stays reserved and unusable until the program ends. In long-running programs, repeated leaks can gradually exhaust available memory.
How do you allocate and free a dynamic array in C++?
You allocate with new type[size], such as int *a = new int[n];, and you must free it with the array form delete[] a;. Using plain delete on an array allocated with new[] is undefined behaviour.
Why are smart pointers preferred over new and delete in C++?
Smart pointers such as unique_ptr and shared_ptr free their memory automatically when they go out of scope, so you cannot forget to call delete. This prevents most leaks and dangling pointers, which is why modern C++ prefers them over raw new/delete.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicStructures in C++ Your progress: 27 / 81 (33%) · Lesson 28 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free