🔵 Core C++  ·  Lesson 27

Dynamic Memory: new and delete

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 pointersunique_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.

Dynamic memory क्यों?

सामान्य variables का size तब तय होता है जब आप code लिखते हैं। पर कभी आपको पता चलता है कि कितनी memory चाहिए केवल program चलते समय — जैसे user input से size वाला array। Dynamic memory आपको run time पर heap से memory माँगने देती है।

new से allocate करना

new heap पर memory आरक्षित करता है और उसका pointer लौटाता है:

C++
int *p = new int;      // heap par ek int
*p = 42;
cout << *p << "\n";     // 42

delete से मुक्त करना

Heap memory अपने आप मुक्त नहीं होती। काम पूरा होने पर आपको इसे delete से वापस करना होगा — हर new को एक मिलता-जुलता delete चाहिए:

C++
int *p = new int(42);
cout << *p << "\n";
delete p;              // mukt karein
p = nullptr;           // dangling pointer se bachein
Output:
42

Dynamic arrays

ऐसा array बनाने को new type[size] इस्तेमाल करें जिसका size run time पर तय हो — और इसे delete[] से मुक्त करें:

C++
int n = 5;
int *a = new int[n];        // n ints ka array
for (int i = 0; i < n; i++) a[i] = i * 10;
for (int i = 0; i < n; i++) cout << a[i] << " ";
delete[] a;                 // arrays ke liye [] dhyan dein
Output:
0 10 20 30 40
💡 रूप मिलाएँ

new delete से जोड़ी बनाता है; new[] delete[] से। इन्हें मिलाना undefined behaviour है।

Memory leaks और dangling pointers

  • Memory leak delete करना भूलना है, तो memory हमेशा के लिए आरक्षित रहती है।
  • Dangling pointer ऐसा pointer है जो memory मुक्त होने के बाद भी वह address रखता है — इसे इस्तेमाल करना undefined behaviour है।

delete के तुरंत बाद pointer को nullptr पर सेट करना आकस्मिक dangling इस्तेमाल से बचाता है।

Smart pointers

आधुनिक C++ smart pointers पसंद करता है — <memory> से unique_ptr और shared_ptr — जो scope से बाहर जाने पर अपनी memory अपने आप मुक्त कर देते हैं, तो आप delete भूल नहीं सकते:

C++
#include <memory>
auto p = make_unique<int>(42);   // khud mukt ho jata hai
cout << *p;

विवरण के लिए smart pointers lesson देखें।

आम गलतियाँ

  • delete भूलना — memory leak।
  • वही pointer दो बार delete करना (double free)।
  • Arrays के लिए delete[] के बजाय delete इस्तेमाल करना।
  • Pointer को delete होने के बाद इस्तेमाल करना (dangling pointer)।
🏋️ अभ्यास

User से size पूछें, उस size का int array dynamically allocate करें, उसे squares (1, 4, 9, ...) से भरें, print करें, फिर delete[] से सही तरीके से मुक्त करें। फिर इसे make_unique से दोबारा लिखें।

सारांश

  • Dynamic memory run time पर heap पर new से allocate होती है।
  • हर new को मिलता-जुलता delete चाहिए; new[] को delete[]
  • मुक्त करना भूलना memory leak पैदा करता है; मुक्त memory इस्तेमाल करना dangling pointer है।
  • Delete करने के बाद pointers को nullptr पर सेट करें।
  • Smart pointers पसंद करें, जो memory अपने आप मुक्त करते हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में dynamic memory क्या है?
Dynamic memory वह memory है जो compile time पर तय होने के बजाय run time पर heap नामक क्षेत्र से new का उपयोग करके allocate होती है। यह program को चलते समय ठीक उतनी memory माँगने देता है जितनी चाहिए — जैसे ऐसा array जिसका size केवल user input से पता हो।
C++ में new और delete में क्या अंतर है?
new heap पर memory allocate करता है और उसका pointer लौटाता है, जबकि delete उस memory को मुक्त करता है जो new ने allocate की। हर new को ठीक एक delete (या arrays के लिए delete[]) से मिलाना चाहिए ताकि memory leak न हो।
C++ में memory leak क्या है?
Memory leak तब होता है जब आप new से memory allocate करें पर उसे कभी delete न करें, तो memory program के खत्म होने तक आरक्षित और अनुपयोगी रहती है। लंबे समय चलने वाले programs में, बार-बार leaks धीरे-धीरे उपलब्ध memory खत्म कर सकते हैं।
C++ में dynamic array कैसे allocate और मुक्त करते हैं?
आप new type[size] से allocate करते हैं, जैसे int *a = new int[n];, और आपको इसे array रूप delete[] a; से मुक्त करना होगा। new[] से allocate किए array पर सादा delete इस्तेमाल करना undefined behaviour है।
C++ में new और delete के बजाय smart pointers क्यों पसंद किए जाते हैं?
unique_ptr और shared_ptr जैसे smart pointers scope से बाहर जाने पर अपनी memory अपने आप मुक्त कर देते हैं, तो आप delete call करना भूल नहीं सकते। यह अधिकांश leaks और dangling pointers रोकता है, इसीलिए आधुनिक C++ इन्हें 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.

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