<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: xenon</title>
    <description>The latest articles on DEV Community by xenon (@xenon54).</description>
    <link>https://dev.to/xenon54</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4101495%2Fb39a9187-6d9f-413c-9c1c-ec9b82ad91b6.png</url>
      <title>DEV Community: xenon</title>
      <link>https://dev.to/xenon54</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xenon54"/>
    <language>en</language>
    <item>
      <title>Memory in C++</title>
      <dc:creator>xenon</dc:creator>
      <pubDate>Wed, 02 Sep 2026 07:56:23 +0000</pubDate>
      <link>https://dev.to/xenon54/memory-in-c-1i7b</link>
      <guid>https://dev.to/xenon54/memory-in-c-1i7b</guid>
      <description>&lt;h1&gt;
  
  
  Understanding Memory in C++
&lt;/h1&gt;

&lt;p&gt;Normally, whenever we hear the term &lt;strong&gt;memory&lt;/strong&gt;, we think of a part of computer hardware that is faster than RAM, or we may think of the place where the CPU keeps the data it is currently working on.&lt;/p&gt;

&lt;p&gt;But things are a little different when we talk about &lt;strong&gt;memory in C++&lt;/strong&gt;. In C++, the term covers many concepts related to how our program uses memory to manage the data that the CPU works with.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;RAM&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Let's first understand what happens when a program gets loaded into memory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Program memory

┌──────────────────────┐
│ Code                 │
├──────────────────────┤
│ Global / Static      │
├──────────────────────┤
│ Heap                 │
├──────────────────────┤
│                      │
│ Stack                │
└──────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stack:&lt;/strong&gt; Fast, automatic memory used for local variables and function execution frames. When a function ends, the objects with automatic storage duration in its stack frame are destroyed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Heap (Free Store):&lt;/strong&gt; Memory used for dynamic storage. In C++, it can be allocated using &lt;code&gt;new&lt;/code&gt; and released using &lt;code&gt;delete&lt;/code&gt;. This memory persists until it is released, making manually managed dynamic memory a common source of memory leaks if it is not properly released.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Global / Static Data:&lt;/strong&gt; Stores global and static variables that remain alive for the lifetime of the program.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Code (Text) Segment:&lt;/strong&gt; A section of the program's memory that holds the compiled machine-code instructions. It is typically read-only.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Code (Text) Segment&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This is a read-only segment. The OS does not allow us to write here.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Global / Static Data&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Variables&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. &lt;strong&gt;Global Declaration&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;We can allocate data here by declaring variables at global scope. The scope of these variables is global, so we can access them from different parts of the code according to their scope and linkage.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. &lt;strong&gt;Static Allocation&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This is one of the most important parts to understand.&lt;/p&gt;

&lt;p&gt;Whenever we declare a static variable, it has &lt;strong&gt;static storage duration&lt;/strong&gt;. This means that once it is initialized, it remains alive until the program terminates.&lt;/p&gt;

&lt;p&gt;But there is one catch: the &lt;strong&gt;scope&lt;/strong&gt; of a static variable is not necessarily the same as that of a global variable.&lt;/p&gt;

&lt;p&gt;It follows the normal rules of scope.&lt;/p&gt;

&lt;p&gt;When we declare a static variable inside a function and call that function for the first time, the static variable is initialized and remains alive for the entire program execution. Other local variables in the function have automatic storage duration.&lt;/p&gt;

&lt;p&gt;When the function ends, the automatic local variables are destroyed, but the variable with the &lt;code&gt;static&lt;/code&gt; keyword remains alive.&lt;/p&gt;

&lt;p&gt;When we call the function again, the static variable is not created again. The same variable is used, and it retains its previous state or data, which the function can continue to work with.&lt;/p&gt;

&lt;p&gt;But this does not mean we can access this variable outside the function.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. &lt;strong&gt;Static Functions&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This is something that needs to be clarified.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;static&lt;/code&gt; for functions means something different from what we know about static variables.&lt;/p&gt;

&lt;p&gt;When we add &lt;code&gt;static&lt;/code&gt; to a function at file or namespace scope, it gives the function &lt;strong&gt;internal linkage&lt;/strong&gt;. This means that the function cannot be accessed from other &lt;code&gt;.cpp&lt;/code&gt; files.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What About OOP?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Here, the same concept applies to variables.&lt;/p&gt;

&lt;p&gt;When we have a static variable inside a class, it is &lt;strong&gt;not part of each object&lt;/strong&gt;. It belongs to the class, and all objects can access and modify the same variable.&lt;/p&gt;

&lt;p&gt;For functions, it is different.&lt;/p&gt;

&lt;p&gt;Static member functions are associated with the class, but they do not have access to the &lt;code&gt;this&lt;/code&gt; pointer.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Note&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Static variables are not manually deleted. Their lifetime ends automatically according to the rules of their storage duration, and objects with static storage duration are destroyed during program termination.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Rule of Thumb&lt;/strong&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't use &lt;code&gt;static&lt;/code&gt; merely because you want to avoid creating a variable repeatedly. Use it because the variable logically needs a persistent lifetime.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Global Variables&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Be more careful with these.&lt;/p&gt;

&lt;h3&gt;
  
  
  Global Mutable State
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;anywhere → can modify it

        ↓

harder to understand who changed it

        ↓

harder to test/debug
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Prefer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;local variables&lt;/li&gt;
&lt;li&gt;function parameters&lt;/li&gt;
&lt;li&gt;objects&lt;/li&gt;
&lt;li&gt;encapsulated class members&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;const&lt;/code&gt; / &lt;code&gt;constexpr&lt;/code&gt; globals when appropriate&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A global constant is generally much less problematic than a mutable global.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Stack and Heap&lt;/strong&gt;
&lt;/h1&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Stack&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The stack is primarily used for &lt;strong&gt;automatic storage&lt;/strong&gt; associated with function execution.&lt;/p&gt;

&lt;p&gt;This is where function-local variables with automatic storage duration are typically stored while their function is executing.&lt;/p&gt;

&lt;p&gt;When we have nested functions, we can have multiple function execution frames active on the stack.&lt;/p&gt;

&lt;p&gt;It follows the &lt;strong&gt;Last In, First Out (LIFO)&lt;/strong&gt; rule.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;x&lt;/code&gt; is a local variable with automatic storage duration if it is declared inside a function.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Heap&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;The heap is used for &lt;strong&gt;dynamic storage&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Stack

┌──────────────┐
│ p            │ ────────────────┐
└──────────────┘                 │
                                 ↓
                           ┌──────────────┐
                           │      10      │  Heap
                           └──────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The object created by &lt;code&gt;new&lt;/code&gt; has &lt;strong&gt;dynamic storage duration&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Stack is fast, heap is slow."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's an oversimplification.&lt;/p&gt;

&lt;p&gt;The real performance differences come from &lt;strong&gt;allocation mechanisms, access patterns, locality, allocator behavior, cache behavior, etc.&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Ways of Allocating Memory&lt;/strong&gt;
&lt;/h1&gt;

&lt;h2&gt;
  
  
  1. &lt;code&gt;new&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The most direct way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. &lt;code&gt;malloc()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;C++ can also use the C allocation functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;

&lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 malloc(sizeof(int))
                         │
                         ▼
                   Allocate N bytes
                         │
                         ▼
                ┌──────────────┐
                │ raw memory   │
                │              │
                │   N bytes    │
                └──────────────┘
                         ▲
                         │
                       void*
                         │
                         ▼
                 static_cast&amp;lt;int*&amp;gt;
                         │
                         ▼
                       int*
                         │
                         ▼
                         p
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can then store data at that memory location:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. &lt;code&gt;std::allocator&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;C++ also provides &lt;code&gt;std::allocator&lt;/code&gt; for allocating raw storage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;allocator&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;allocate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;construct_at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;destroy_at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deallocate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The basic process is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;allocate
   ↓
construct
   ↓
use
   ↓
destroy
   ↓
deallocate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;strong&gt;Standard Template Library (STL)&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;The STL provides many tools that help us manage memory and resources.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;1. Smart Pointers — Memory Ownership&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Defined in &lt;code&gt;&amp;lt;memory&amp;gt;&lt;/code&gt;, smart pointers manage dynamically allocated objects and automatically release them according to their ownership rules.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;std::unique_ptr&lt;/code&gt;:&lt;/strong&gt; Exclusive ownership. Exactly one &lt;code&gt;unique_ptr&lt;/code&gt; owns the resource. It cannot be copied, only moved.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;std::shared_ptr&lt;/code&gt;:&lt;/strong&gt; Shared ownership. It uses reference counting to track how many &lt;code&gt;shared_ptr&lt;/code&gt; objects share ownership of the resource. The resource is deleted when the last &lt;code&gt;shared_ptr&lt;/code&gt; owning it is destroyed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;std::weak_ptr&lt;/code&gt;:&lt;/strong&gt; A non-owning reference to an object managed by a &lt;code&gt;std::shared_ptr&lt;/code&gt;. It is commonly used to break circular dependencies.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;memory&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Resource&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Acquired&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Released&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;example&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// std::make_unique is preferred for safety and convenience&lt;/span&gt;
    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;uPtr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// Shared ownership&lt;/span&gt;
    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sPtr1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_shared&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sPtr2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sPtr1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Reference count increases to 2&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// All resources are automatically released here&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;2. Automatic Container Memory&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Containers like &lt;code&gt;std::vector&lt;/code&gt;, &lt;code&gt;std::string&lt;/code&gt;, &lt;code&gt;std::map&lt;/code&gt;, and &lt;code&gt;std::list&lt;/code&gt; manage their underlying dynamic storage automatically.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;RAII Guarantee:&lt;/strong&gt; Allocations happen during construction or insertion, and cleanup happens automatically when the container is destroyed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Capacity Control:&lt;/strong&gt; Containers like &lt;code&gt;std::vector&lt;/code&gt; can allocate extra space to minimize expensive reallocations.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Best Practices&lt;/strong&gt;
&lt;/h1&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Technique&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Goal&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Action&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Prefer Smart Pointers&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Avoid raw &lt;code&gt;new&lt;/code&gt; / &lt;code&gt;delete&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Use &lt;code&gt;std::make_unique&lt;/code&gt; or &lt;code&gt;std::make_shared&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Use &lt;code&gt;reserve()&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Avoid repeated heap allocations&lt;/td&gt;
&lt;td&gt;Call &lt;code&gt;vec.reserve()&lt;/code&gt; before pushing a large number of items into a &lt;code&gt;std::vector&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Pass by Reference&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Reduce memory copies&lt;/td&gt;
&lt;td&gt;Pass large STL containers as &lt;code&gt;const T&amp;amp;&lt;/code&gt; or move them using &lt;code&gt;std::move&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Clear Memory Explicitly&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Free unused capacity immediately&lt;/td&gt;
&lt;td&gt;Use &lt;code&gt;vec.shrink_to_fit()&lt;/code&gt; or the swap trick (&lt;code&gt;std::vector&amp;lt;T&amp;gt;().swap(vec)&lt;/code&gt;).&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Thanks for reading. &lt;br&gt;
By Xenon. &lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>cpp</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
