<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Smart Pointer on Cheat Sheet</title><link>/tags/smart-pointer/</link><description>Recent content in Smart Pointer on Cheat Sheet</description><generator>Hugo</generator><language>en</language><atom:link href="/tags/smart-pointer/index.xml" rel="self" type="application/rss+xml"/><item><title>Smart Pointers</title><link>/programming/cpp/primer/smart-pointers/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/smart-pointers/</guid><description>&lt;h1 id="smart-pointers"&gt;Smart Pointers&lt;a class="anchor" href="#smart-pointers"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Summary based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; Chapter 12 (Dynamic Memory and Smart Pointers).&lt;/p&gt;
&lt;h2 id="1-why-smart-pointers-exist-raii-and-ownership"&gt;1. Why Smart Pointers Exist (RAII and Ownership)&lt;a class="anchor" href="#1-why-smart-pointers-exist-raii-and-ownership"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Stack vs. Heap Lifetimes:&lt;/strong&gt; Stack local variables automatically invoke their destructor (&lt;code&gt;~Class()&lt;/code&gt;) when leaving scope (&lt;code&gt;{ ... }&lt;/code&gt;), whereas heap memory allocated via &lt;code&gt;new&lt;/code&gt; is NOT automatically freed when a raw pointer (&lt;code&gt;T*&lt;/code&gt;) goes out of scope, leading to memory leaks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="../../programming/cpp/raii/"&gt;RAII (Resource Acquisition Is Initialization)&lt;/a&gt;:&lt;/strong&gt; By wrapping a raw pointer (&lt;code&gt;T*&lt;/code&gt;) inside a &lt;strong&gt;stack-allocated wrapper class&lt;/strong&gt; whose &lt;strong&gt;destructor (&lt;code&gt;~SmartPtr()&lt;/code&gt;) automatically calls &lt;code&gt;delete ptr;&lt;/code&gt;&lt;/strong&gt;, heap memory is deterministically freed when the wrapper leaves scope.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Copy / Double-Free Problem and Ownership Models:&lt;/strong&gt; If two smart pointers pointed to the same heap address and both called &lt;code&gt;delete&lt;/code&gt; in their destructors, a fatal &lt;strong&gt;Double Free&lt;/strong&gt; crash would occur. To solve this, C++11 &lt;code&gt;&amp;lt;memory&amp;gt;&lt;/code&gt; provides three smart pointers with distinct ownership semantics:&lt;/li&gt;
&lt;/ul&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Smart Pointer&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Ownership Model&lt;/th&gt;
 &lt;th style="text-align: center"&gt;Copyable?&lt;/th&gt;
 &lt;th style="text-align: center"&gt;&lt;code&gt;*ptr&lt;/code&gt; / &lt;code&gt;ptr-&amp;gt;&lt;/code&gt; Dereference&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Key Characteristics &amp;amp; Use Case&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;std::unique_ptr&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Exclusive Ownership&lt;/strong&gt; — 1:1&lt;/td&gt;
 &lt;td style="text-align: center"&gt;❌ &lt;code&gt;std::move&lt;/code&gt; only&lt;/td&gt;
 &lt;td style="text-align: center"&gt;✅&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Zero overhead with the same size and speed as &lt;code&gt;T*&lt;/code&gt;; the default choice for dynamic memory.&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;std::shared_ptr&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Shared Ownership&lt;/strong&gt; — N:1&lt;/td&gt;
 &lt;td style="text-align: center"&gt;✅ updates &lt;code&gt;use_count&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: center"&gt;✅&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Calls &lt;code&gt;delete&lt;/code&gt; only when the strong reference count &lt;code&gt;use_count&lt;/code&gt; in the Control Block reaches 0.&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;std::weak_ptr&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Non-Owning&lt;/strong&gt; — Observer&lt;/td&gt;
 &lt;td style="text-align: center"&gt;✅ &lt;code&gt;use_count&lt;/code&gt; unchanged&lt;/td&gt;
 &lt;td style="text-align: center"&gt;❌ &lt;code&gt;.lock()&lt;/code&gt; required&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Observes a &lt;code&gt;shared_ptr&lt;/code&gt;-managed object without incrementing &lt;code&gt;use_count&lt;/code&gt;, breaking &lt;strong&gt;circular reference memory leaks&lt;/strong&gt;.&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id="2-stdunique_ptr-exclusive-ownership-and-stdmake_unique"&gt;2. &lt;code&gt;std::unique_ptr&lt;/code&gt;: Exclusive Ownership and &lt;code&gt;std::make_unique&lt;/code&gt;&lt;a class="anchor" href="#2-stdunique_ptr-exclusive-ownership-and-stdmake_unique"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Because only one &lt;code&gt;unique_ptr&lt;/code&gt; can own a given object at a time, its copy constructor and copy-assignment operator are deleted (&lt;code&gt;= delete&lt;/code&gt;).&lt;/p&gt;</description></item></channel></rss>