C++ Programming 5. C++ Pointers and References 18 mins read 25,606 views

C++ Memory Management: Allocation, Ownership, and Lifetime Concepts

Memory management is an important part of C++ programming. Every C++ program uses memory to store variables, objects, arrays, and other data while it runs.Under...
Published: Nov 1, 2026Last Updated: Nov 21, 2026Curriculum: Class 12 / AP Physics / JEE-NEETReviewed by: OperateTools Editorial Board
Quick Summary & Direct Answer (AEO / GEO Extraction)

What is C++ Memory Management: Allocation, Ownership, and Lifetime Concepts? Memory management is an important part of C++ programming. Every C++ program uses memory to store variables, objects, arrays, and other data while it runs.Under...

Audience & Scope: Students, educators, and candidates preparing for academic examinations and technical assessments in C++ Programming.
How does C++ manage Stack, Heap, Static, and Dynamic memory allocations with deterministic lifespans?
<p data-pm-slice="1 1 []">Memory management is an important part of C++ programming. Every C++ program uses memory to store variables, objects, arrays, and other data while it runs.</p><p data-pm-slice="1 1 []"><br></p><p>Understanding how memory is allocated, accessed, and released helps you write efficient, reliable, and safe programs.</p><p><br></p><p>C++ provides different ways to manage memory. Some memory is managed automatically, while dynamically allocated memory requires appropriate ownership and lifetime management.</p><p><br></p><p>In modern C++, tools such as <code dir="ltr">std::vector</code>, <code dir="ltr">std::unique_ptr</code>, and <code dir="ltr">std::shared_ptr</code> make memory management safer and easier.</p>

Explanation & In-Depth Details

<h2 class="h4 fw-bold text-dark mb-3"><span style="font-size: 1.15rem;">1. What Is Memory Management in C++?</span></h2><p>Memory management is the process of allocating, using, and releasing memory during program execution.</p><p>A program needs memory to store:</p><ul><li><p>Variables and constants.</p></li><li><p>Objects and class instances.</p></li><li><p>Arrays and strings.</p></li><li><p>Function call information.</p></li><li><p>Dynamically created data structures.</p></li><li><p>Temporary values and intermediate results.</p></li></ul><p>C++ supports automatic storage management and dynamic memory allocation.</p><h3>Example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Number: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">number</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Number</span><span class="js-op">:</span> <span class="js-num">100</span></code></pre></div><p>Here, <code>number</code> is an automatic local variable. Its lifetime ends when execution leaves its scope.</p><p>The compiler and runtime manage its storage automatically.</p><h2>2. Understanding Memory Areas in C++</h2><p>A C++ program commonly uses several categories of storage.</p><p>The most important concepts for beginners are:</p><ol start="1"><li><p>Automatic storage duration.</p></li><li><p>Static storage duration.</p></li><li><p>Thread storage duration.</p></li><li><p>Dynamic storage duration.</p></li></ol><p>The terms <em>stack</em> and <em>heap</em> are commonly used to describe implementation-level memory areas, but they are not the same thing as the C++ storage-duration rules.</p><h3>Common memory areas</h3><table><tbody><tr><th>Memory area</th><th>Typical purpose</th><th>Lifetime</th></tr><tr><td>Stack</td><td>Common implementation for function call frames and local automatic objects</td><td>Usually until the scope ends</td></tr><tr><td>Heap / free store</td><td>Common implementation for dynamically allocated objects</td><td>Until released or otherwise managed</td></tr><tr><td>Static storage</td><td>Global variables and static local variables</td><td>Entire program execution</td></tr><tr><td>Thread-local storage</td><td>Objects declared with <code>thread_local</code></td><td>Lifetime of the associated thread</td></tr></tbody></table><h3>Understanding the stack</h3><p>The stack is commonly used for function calls and local automatic variables.</p><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-kw">void</span> <span class="js-id">display</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">50</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">number</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-op">}</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">display</span><span class="js-op">();</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">50</span></code></pre></div><p>The local variable <code>number</code> has automatic storage duration. Its lifetime ends when <code>display()</code> finishes.</p><h3>Understanding dynamic memory</h3><p>Dynamic memory is allocated during program execution.</p><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-kw">new</span> <span class="js-id">int</span><span class="js-op">(</span><span class="js-num">100</span><span class="js-op">);</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-id">delete</span> <span class="js-id">ptr</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">100</span></code></pre></div><p>Here:</p><ul><li><p><code>new int(100)</code> creates a dynamically allocated integer.</p></li><li><p><code>ptr</code> stores its address.</p></li><li><p><code>*ptr</code> accesses its value.</p></li><li><p><code>delete ptr</code> releases the allocated memory.</p></li></ul><h2>3. Types of Memory Allocation in C++</h2><p>C++ supports different approaches to allocating memory.</p><h3>1. Static allocation</h3><p>Static storage is used for global variables, static local variables, and certain other objects.</p><p>Their storage lasts for the entire program execution.</p><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">total</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">static</span> <span class="js-id">int</span> <span class="js-id">count</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">total</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">count</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">100</span>
<span class="js-num">10</span></code></pre></div><h3>2. Automatic allocation</h3><p>Automatic objects are typically created when execution enters their scope and destroyed when execution leaves that scope.</p><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">age</span> <span class="js-op">=</span> <span class="js-num">25</span><span class="js-op">;</span>
<span class="js-id">double</span> <span class="js-id">salary</span> <span class="js-op">=</span> <span class="js-num">50000</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">age</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">salary</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>The variables have automatic storage duration.</p><h3>3. Dynamic allocation</h3><p>Dynamic memory allocation allows objects to be created during program execution.</p><p>C++ provides <code>new</code> and <code>delete</code> for direct dynamic allocation.</p><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-kw">new</span> <span class="js-id">int</span><span class="js-op">(</span><span class="js-num">250</span><span class="js-op">);</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-id">delete</span> <span class="js-id">ptr</span><span class="js-op">;</span>
<span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">nullptr</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">250</span></code></pre></div><p>Dynamic allocation is useful when the required size or lifetime of an object cannot be conveniently determined using ordinary local variables.</p><h2>4. Dynamic Memory Allocation Using new</h2><p>The <code>new</code> operator allocates storage for an object and initializes it.</p><h3>Syntax</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">data_type</span><span class="js-op">*</span> <span class="js-id">pointer</span> <span class="js-op">=</span> <span class="js-kw">new</span> <span class="js-id">data_type</span><span class="js-op">;</span></code></pre></div><p>You can also initialize the object while allocating it.</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">data_type</span><span class="js-op">*</span> <span class="js-id">pointer</span> <span class="js-op">=</span> <span class="js-kw">new</span> <span class="js-id">data_type</span><span class="js-op">(</span><span class="js-id">value</span><span class="js-op">);</span></code></pre></div><h3>Example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-kw">new</span> <span class="js-id">int</span><span class="js-op">(</span><span class="js-num">500</span><span class="js-op">);</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Value: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-id">delete</span> <span class="js-id">ptr</span><span class="js-op">;</span>
<span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">nullptr</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Value</span><span class="js-op">:</span> <span class="js-num">500</span></code></pre></div><h3>Explanation</h3><ul><li><p><code>new int(500)</code> allocates and initializes an integer.</p></li><li><p>The returned pointer stores the object's address.</p></li><li><p><code>*ptr</code> accesses the allocated integer.</p></li><li><p><code>delete ptr</code> destroys the object and releases its storage.</p></li></ul><p>If allocation fails, a normal throwing <code>new</code> expression generally throws <code>std::bad_alloc</code>.</p><h2>5. Releasing Dynamic Memory Using delete</h2><p>Memory allocated using a single-object <code>new</code> expression should be released using <code>delete</code>.</p><h3>Example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-kw">new</span> <span class="js-id">int</span><span class="js-op">(</span><span class="js-num">100</span><span class="js-op">);</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-id">delete</span> <span class="js-id">ptr</span><span class="js-op">;</span>
<span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">nullptr</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">100</span></code></pre></div><p>The <code>delete</code> expression ends the lifetime of the dynamically allocated object and releases its storage.</p><h3>Important rules</h3><ul><li><p>Use <code>delete</code> for memory allocated with a single-object <code>new</code>.</p></li><li><p>Use <code>delete[]</code> for memory allocated with <code>new[]</code>.</p></li><li><p>Do not delete the same allocation more than once.</p></li><li><p>Do not use a pointer to access an object after it has been deleted.</p></li><li><p>Do not delete pointers to ordinary local variables or global variables.</p></li></ul><h2>6. Dynamic Memory Allocation for Arrays</h2><p>You can allocate arrays dynamically when the required number of elements is known only at runtime.</p><p>Use <code>new[]</code> to allocate an array and <code>delete[]</code> to release it.</p><h3>Example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">size</span> <span class="js-op">=</span> <span class="js-num">5</span><span class="js-op">;</span>

<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">numbers</span> <span class="js-op">=</span> <span class="js-kw">new</span> <span class="js-id">int</span>[<span class="js-id">size</span>]<span class="js-op">;</span>

<span class="js-kw">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span> <span class="js-id">i</span> <span class="js-op">&lt;</span> <span class="js-id">size</span><span class="js-op">;</span> <span class="js-id">i</span><span class="js-op">++)</span> <span class="js-op">{</span>
<span class="js-id">numbers</span>[<span class="js-id">i</span>] <span class="js-op">=</span> <span class="js-op">(</span><span class="js-id">i</span> <span class="js-op">+</span> <span class="js-num">1</span><span class="js-op">)</span> <span class="js-op">*</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-op">}</span>

<span class="js-kw">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span> <span class="js-id">i</span> <span class="js-op">&lt;</span> <span class="js-id">size</span><span class="js-op">;</span> <span class="js-id">i</span><span class="js-op">++)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">numbers</span>[<span class="js-id">i</span>] <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-op">}</span>

<span class="js-id">delete</span>[] <span class="js-id">numbers</span><span class="js-op">;</span>
<span class="js-id">numbers</span> <span class="js-op">=</span> <span class="js-id">nullptr</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">10</span>
<span class="js-num">20</span>
<span class="js-num">30</span>
<span class="js-num">40</span>
<span class="js-num">50</span></code></pre></div><h3>Explanation</h3><ol start="1"><li><p>The program stores the required array size in <code>size</code>.</p></li><li><p><code>new int[size]</code> allocates an array of five integers.</p></li><li><p>The first loop assigns values.</p></li><li><p>The second loop prints the values.</p></li><li><p><code>delete[] numbers</code> releases the array allocation.</p></li></ol><p><strong>Important:</strong> Always match <code>new[]</code> with <code>delete[]</code>. Using the wrong form of delete results in undefined behavior.</p><h2>7. Understanding Object Lifetime in C++</h2><p>Object lifetime describes the period during which an object exists and can be used safely.</p><p>An object's lifetime depends on how and where it is created.</p><h3>Automatic object lifetime</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-kw">void</span> <span class="js-id">show</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">number</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-op">}</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">show</span><span class="js-op">();</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>The local variable exists during the execution of <code>show()</code> and its lifetime ends when the function exits.</p><h3>Static object lifetime</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-kw">void</span> <span class="js-id">counter</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">static</span> <span class="js-id">int</span> <span class="js-id">count</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span>

<span class="js-id">count</span><span class="js-op">++;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">count</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-op">}</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">counter</span><span class="js-op">();</span>
<span class="js-id">counter</span><span class="js-op">();</span>
<span class="js-id">counter</span><span class="js-op">();</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">1</span>
<span class="js-num">2</span>
<span class="js-num">3</span></code></pre></div><p>The static local variable is initialized once and retains its value between function calls. Its lifetime lasts until program termination.</p><h3>Dynamic object lifetime</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-kw">new</span> <span class="js-id">int</span><span class="js-op">(</span><span class="js-num">100</span><span class="js-op">);</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-id">delete</span> <span class="js-id">ptr</span><span class="js-op">;</span>
<span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">nullptr</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>The dynamically allocated integer exists until its lifetime ends through deletion or another applicable lifetime-ending operation.</p><h2>8. Memory Ownership in C++</h2><p>Memory ownership describes which part of a program is responsible for managing an object's lifetime.</p><p>When working with dynamic memory, ownership is especially important.</p><p>For example, if a program allocates memory but loses the only pointer that can release it, the allocation may become inaccessible and cause a memory leak.</p><h3>Common ownership models</h3><table><tbody><tr><th>Ownership type</th><th>Meaning</th><th>Common C++ tool</th></tr><tr><td>Automatic ownership</td><td>Object lifetime follows its scope</td><td>Local variables</td></tr><tr><td>Exclusive ownership</td><td>One owner manages the object</td><td><code>std::unique_ptr</code></td></tr><tr><td>Shared ownership</td><td>Multiple owners share lifetime responsibility</td><td><code>std::shared_ptr</code></td></tr><tr><td>Non-owning access</td><td>Access without controlling lifetime</td><td>Raw pointers and references</td></tr><tr><td>Weak ownership</td><td>Observes a shared object without keeping it alive</td><td><code>std::weak_ptr</code></td></tr></tbody></table><h3>Example: Exclusive ownership</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">memory</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">unique_ptr</span><span class="js-op">&lt;</span><span class="js-id">int</span><span class="js-op">&gt;</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">make_unique</span><span class="js-op">&lt;</span><span class="js-id">int</span><span class="js-op">&gt;(</span><span class="js-num">100</span><span class="js-op">);</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">100</span></code></pre></div><p>The <code>unique_ptr</code> automatically releases the dynamically allocated integer when the pointer goes out of scope.</p><h2>9. Understanding RAII in C++</h2><p>RAII stands for <strong>Resource Acquisition Is Initialization</strong>.</p><p>It is a C++ programming technique in which a resource is acquired during object initialization and released automatically when the owning object is destroyed.</p><p>RAII is one of the most important principles for safe C++ memory management.</p><h3>Example using RAII</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">memory</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">unique_ptr</span><span class="js-op">&lt;</span><span class="js-id">int</span><span class="js-op">&gt;</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-id">make_unique</span><span class="js-op">&lt;</span><span class="js-id">int</span><span class="js-op">&gt;(</span><span class="js-num">500</span><span class="js-op">);</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">number</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">500</span></code></pre></div><h3>How does RAII work?</h3><ol start="1"><li><p>The <code>unique_ptr</code> object is created.</p></li><li><p>It takes ownership of a dynamically allocated integer.</p></li><li><p>The program uses the object through the smart pointer.</p></li><li><p>When the <code>unique_ptr</code> goes out of scope, its destructor runs.</p></li><li><p>The allocated integer is released automatically.</p></li></ol><p>RAII is also useful for managing files, locks, sockets, and other resources.</p><h2>10. Smart Pointers in C++</h2><p>Smart pointers are C++ library objects that help manage dynamically allocated memory automatically.</p><p>They are available through the <code>&lt;memory&gt;</code> header.</p><p>The three main smart pointer types are:</p><ol start="1"><li><p><code>std::unique_ptr</code></p></li><li><p><code>std::shared_ptr</code></p></li><li><p><code>std::weak_ptr</code></p></li></ol><h3>1. unique_ptr</h3><p>A <code>unique_ptr</code> provides exclusive ownership of an object.</p><p>It cannot be copied, but ownership can be transferred using move semantics.</p><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">memory</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">unique_ptr</span><span class="js-op">&lt;</span><span class="js-id">int</span><span class="js-op">&gt;</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">make_unique</span><span class="js-op">&lt;</span><span class="js-id">int</span><span class="js-op">&gt;(</span><span class="js-num">100</span><span class="js-op">);</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">100</span></code></pre></div><p>When <code>ptr</code> goes out of scope, its managed object is automatically destroyed.</p><h3>2. shared_ptr</h3><p>A <code>shared_ptr</code> allows multiple smart pointers to share ownership of an object.</p><p>The managed object is destroyed when the last owning <code>shared_ptr</code> is released.</p><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">memory</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">shared_ptr</span><span class="js-op">&lt;</span><span class="js-id">int</span><span class="js-op">&gt;</span> <span class="js-id">first</span> <span class="js-op">=</span> <span class="js-id">make_shared</span><span class="js-op">&lt;</span><span class="js-id">int</span><span class="js-op">&gt;(</span><span class="js-num">200</span><span class="js-op">);</span>

<span class="js-id">shared_ptr</span><span class="js-op">&lt;</span><span class="js-id">int</span><span class="js-op">&gt;</span> <span class="js-id">second</span> <span class="js-op">=</span> <span class="js-id">first</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">first</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">second</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Owners: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">first</span><span class="js-op">.</span><span class="js-id">use_count</span><span class="js-op">()</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">200</span>
<span class="js-num">200</span>
<span class="js-id">Owners</span><span class="js-op">:</span> <span class="js-num">2</span></code></pre></div><p>Both pointers share ownership of the same integer.</p><h3>3. weak_ptr</h3><p>A <code>weak_ptr</code> observes an object managed by <code>shared_ptr</code> without increasing its strong ownership count.</p><p>It is useful for avoiding reference cycles.</p><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">memory</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">shared_ptr</span><span class="js-op">&lt;</span><span class="js-id">int</span><span class="js-op">&gt;</span> <span class="js-id">owner</span> <span class="js-op">=</span> <span class="js-id">make_shared</span><span class="js-op">&lt;</span><span class="js-id">int</span><span class="js-op">&gt;(</span><span class="js-num">300</span><span class="js-op">);</span>

<span class="js-id">weak_ptr</span><span class="js-op">&lt;</span><span class="js-id">int</span><span class="js-op">&gt;</span> <span class="js-id">observer</span> <span class="js-op">=</span> <span class="js-id">owner</span><span class="js-op">;</span>

<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">auto</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">observer</span><span class="js-op">.</span><span class="js-id">lock</span><span class="js-op">())</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-op">}</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">300</span></code></pre></div><p>The <code>lock()</code> function attempts to obtain a temporary <code>shared_ptr</code>. If the object has already been destroyed, it returns an empty pointer.</p><h2>11. Memory Leaks in C++</h2><p>A memory leak happens when dynamically allocated memory is no longer properly accessible or released while the program continues running.</p><p>Memory leaks can gradually increase memory usage and reduce application performance.</p><h3>Example of a memory leak</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-kw">new</span> <span class="js-id">int</span><span class="js-op">(</span><span class="js-num">100</span><span class="js-op">);</span>

<span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">nullptr</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Here, the allocated integer is not deleted before the pointer is overwritten. The program loses its access to the allocation.</p><h3>Correct approach</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-kw">new</span> <span class="js-id">int</span><span class="js-op">(</span><span class="js-num">100</span><span class="js-op">);</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-id">delete</span> <span class="js-id">ptr</span><span class="js-op">;</span>
<span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">nullptr</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><h3>Better modern C++ approach</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">memory</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">auto</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">make_unique</span><span class="js-op">&lt;</span><span class="js-id">int</span><span class="js-op">&gt;(</span><span class="js-num">100</span><span class="js-op">);</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-op">*</span><span class="js-id">ptr</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">endl</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>The smart pointer automatically releases the managed memory.</p><h2>12. Dangling Pointers in C++</h2><p>A dangling pointer is a pointer that refers to an object whose lifetime has ended or whose storage is no longer valid for access.</p><h3>Example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>

<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-kw">new</span> <span class="js-id">int</span><span class="js-op">(</span><span class="js-num">100</span><span class="js-op">);</span>

<span class="js-id">delete</span> <span class="js-id">ptr</span><span class="js-op">;</span>

<span class="js-cm">// Do not access *ptr here.</span>

<span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">nullptr</span><span class="js-op">;</span>

<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>After <code>delete ptr</code>, the allocated integer no longer exists. The pointer should not be dereferenced.</p><p>Setting the pointer to <code>nullptr</code> prevents accidental use through that particular pointer, but it does not fix other copies of the old address.</p><h3>Common causes</h3><ul><li><p>Deleting an object and continuing to use its pointer.</p></li><li><p>Returning the address of a local variable.</p></li><li><p>Accessing an object after its lifetime ends.</p></li><li><p>Keeping pointers to elements after a container reallocates its storage.</p></li></ul><h2>13. Stack vs Heap Memory in C++</h2><p>Stack and heap are common terms used to describe memory organization.</p><p>They are useful for understanding how programs operate, although the C++ language standard defines storage durations rather than requiring every implementation to use a specific stack and heap layout.</p><h3>Comparison</h3><table><tbody><tr><th>Feature</th><th>Stack</th><th>Heap / free store</th></tr><tr><td>Common use</td><td>Function calls and automatic objects</td><td>Dynamic allocations</td></tr><tr><td>Allocation</td><td>Commonly managed automatically</td><td>Requested dynamically</td></tr><tr><td>Lifetime</td><td>Usually tied to scope</td><td>Controlled by ownership and lifetime rules</td></tr><tr><td>Management</td><td>Automatic</td><td>Manual or managed through RAII</td></tr><tr><td>Typical access</td><td>Local variables</td><td>Pointers and smart pointers</td></tr><tr><td>Common risk</td><td>Stack overflow</td><td>Leaks and invalid access</td></tr></tbody></table><h3>Stack example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span></code></pre></div><h3>Dynamic allocation example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-kw">new</span> <span class="js-id">int</span><span class="js-op">(</span><span class="js-num">100</span><span class="js-op">);</span>

<span class="js-id">delete</span> <span class="js-id">ptr</span><span class="js-op">;</span></code></pre></div><p>Use automatic objects whenever possible. Dynamic allocation should be used when it provides a real benefit.</p><h2>14. Common Memory Management Errors in C++</h2><p>Beginners and experienced developers can both make memory-management mistakes.</p><h3>1. Memory leak</h3><p>Allocated memory is not released when no longer needed.</p><p><strong>Solution:</strong> Prefer RAII and smart pointers.</p><h3>2. Double deletion</h3><p>The same allocation is deleted more than once.</p><p><strong>Solution:</strong> Establish clear ownership and avoid multiple raw pointers independently claiming ownership.</p><h3>3. Dangling pointer</h3><p>A pointer is used after the object it refers to has been destroyed.</p><p><strong>Solution:</strong> Respect object lifetimes and use suitable ownership tools.</p><h3>4. Buffer overflow</h3><p>A program accesses memory outside the valid boundaries of an array or buffer.</p><p><strong>Solution:</strong> Check array sizes and use safer containers such as <code>std::vector</code> and <code>std::array</code>.</p><h3>5. Mismatched allocation and deallocation</h3><p>Using <code>delete</code> for an array allocated with <code>new[]</code>, or using <code>delete[]</code> for a single object allocated with <code>new</code>, causes undefined behavior.</p><p><strong>Solution:</strong> Match allocation and deallocation forms correctly.</p><h3>6. Uninitialized pointer</h3><p>A pointer is used before being assigned a valid address.</p><p><strong>Solution:</strong> Initialize pointers with a valid address or <code>nullptr</code>.</p><h2>15. Manual Memory Management vs Smart Pointers</h2><p>C++ supports both manual memory management and smart-pointer-based ownership.</p><table><tbody><tr><th>Feature</th><th>Manual memory management</th><th>Smart pointers</th></tr><tr><td>Allocation</td><td><code>new</code></td><td>Usually <code>make_unique</code> or <code>make_shared</code></td></tr><tr><td>Release</td><td><code>delete</code> or <code>delete[]</code></td><td>Automatic</td></tr><tr><td>Ownership</td><td>Must be managed manually</td><td>Expressed through ownership types</td></tr><tr><td>Memory leak risk</td><td>Higher if cleanup is missed</td><td>Lower when used correctly</td></tr><tr><td>Complexity</td><td>More responsibility</td><td>Easier resource management</td></tr><tr><td>Common use</td><td>Low-level or specialized code</td><td>Modern application development</td></tr></tbody></table><h3>Manual approach</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span><span class="js-op">*</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-kw">new</span> <span class="js-id">int</span><span class="js-op">(</span><span class="js-num">100</span><span class="js-op">);</span>

<span class="js-id">delete</span> <span class="js-id">ptr</span><span class="js-op">;</span></code></pre></div><h3>Smart pointer approach</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">memory</span><span class="js-op">&gt;</span>

<span class="js-id">auto</span> <span class="js-id">ptr</span> <span class="js-op">=</span> <span class="js-id">std</span><span class="js-op">::</span><span class="js-id">make_unique</span><span class="js-op">&lt;</span><span class="js-id">int</span><span class="js-op">&gt;(</span><span class="js-num">100</span><span class="js-op">);</span></code></pre></div><p><strong>Recommendation:</strong> Prefer automatic objects and standard library containers first. Use smart pointers when dynamic ownership is needed. Use raw <code>new</code> and <code>delete</code> only when there is a specific reason.</p><h2>16. Best Practices for C++ Memory Management</h2><p>Follow these practices to write safer C++ programs:</p><ol start="1"><li><p>Prefer automatic local objects whenever possible.</p></li><li><p>Use <code>std::vector</code> for dynamic arrays.</p></li><li><p>Use <code>std::array</code> for fixed-size arrays.</p></li><li><p>Use <code>std::unique_ptr</code> for exclusive dynamic ownership.</p></li><li><p>Use <code>std::shared_ptr</code> only when shared ownership is necessary.</p></li><li><p>Use <code>std::weak_ptr</code> to observe shared objects without extending their lifetime.</p></li><li><p>Avoid unnecessary manual <code>new</code> and <code>delete</code>.</p></li><li><p>Keep ownership and object lifetimes clear.</p></li><li><p>Never access objects after their lifetime ends.</p></li><li><p>Match <code>new</code> with <code>delete</code> and <code>new[]</code> with <code>delete[]</code>.</p></li><li><p>Use memory-analysis tools to find leaks and invalid accesses.</p></li><li><p>Compile with useful warnings and test edge cases.</p></li></ol><h2>17. Frequently Asked Questions (FAQs)</h2><h3>Q1. What is memory management in C++?</h3><p>Memory management is the process of allocating, using, and releasing memory used by a C++ program.</p><h3>Q2. What are stack and heap memory?</h3><p>Stack and heap are common implementation terms. The stack is typically used for function calls and automatic objects, while the heap or free store is commonly used for dynamically allocated objects.</p><h3>Q3. What is dynamic memory allocation?</h3><p>Dynamic memory allocation allows a program to request storage during execution using facilities such as <code>new</code> and library containers.</p><h3>Q4. What is the difference between new and delete?</h3><p><code>new</code> allocates storage and creates an object. <code>delete</code> destroys a dynamically allocated object and releases its storage.</p><h3>Q5. What is a memory leak?</h3><p>A memory leak occurs when an allocation is no longer properly accessible or released, even though the program no longer needs it.</p><h3>Q6. What is a dangling pointer?</h3><p>A dangling pointer refers to an object whose lifetime has ended or whose storage is no longer valid for access.</p><h3>Q7. What is RAII in C++?</h3><p>RAII is a technique in which resource ownership is tied to object lifetime. Resources are acquired during initialization and released automatically when the owning object is destroyed.</p><h3>Q8. What are smart pointers?</h3><p>Smart pointers are library types that manage dynamic object ownership. C++ provides <code>unique_ptr</code>, <code>shared_ptr</code>, and <code>weak_ptr</code>.</p><h3>Q9. What is the difference between unique_ptr and shared_ptr?</h3><p><code>unique_ptr</code> provides exclusive ownership. <code>shared_ptr</code> allows multiple owners to share responsibility for an object's lifetime.</p><h3>Q10. Is manual memory management necessary in modern C++?</h3><p>Manual memory management is still available and sometimes necessary, but most application code can use automatic objects, standard containers, RAII, and smart pointers.</p>

Code Example

#include <iostream>
#include <memory>

class AudioBuffer {
private:
    int* buffer;
    size_t size;
public:
    AudioBuffer(size_t s) : size(s), buffer(new int[s]{}) {
        std::cout << "AudioBuffer allocated (" << size << " samples)
";
    }
    ~AudioBuffer() {
        delete[] buffer; // Safe deterministic cleanup
        std::cout << "AudioBuffer released from heap
";
    }
};

int main() {
    std::cout << "Entering scope...
";
    {
        // RAII with Smart Pointer - Zero manual delete required
        auto smartAudio = std::make_unique<AudioBuffer>(1024);
    } // smartAudio destroyed here automatically
    std::cout << "Exited scope safely.
";
    return 0;
}

Interview Question & Tips

Q: What is a memory leak and how does RAII prevent it?
A: A memory leak occurs when heap memory is allocated (`new`) but never freed (`delete`), slowly exhausting available RAM. RAII prevents leaks by wrapping heap resources in stack-allocated wrapper objects (like `std::unique_ptr`) whose destructors automatically free the resource when exiting scope.

Q: What happens if you use delete instead of delete[] on an array?
A: Using `delete` on an array allocated with `new[]` causes undefined behavior: only the first element's destructor runs, and heap allocator metadata is corrupted.

Best Practices

1. Never use raw `new` and `delete` in application code; use `std::make_unique` and `std::make_shared`.
2. Adhere to the Rule of Zero / Rule of Five when managing custom class resources.
3. Use memory sanitizers (AddressSanitizer / Valgrind) in test suites to detect memory errors.
Academic Standards & Verified Citations

This study reference for C++ Memory Management: Allocation, Ownership, and Lifetime Concepts is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation:

C++ Programming