<?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: Jaya Prakash</title>
    <description>The latest articles on DEV Community by Jaya Prakash (@jayaprakash20).</description>
    <link>https://dev.to/jayaprakash20</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3267073%2F03cfff2e-7572-436d-91b3-1e44618b9bb7.jpeg</url>
      <title>DEV Community: Jaya Prakash</title>
      <link>https://dev.to/jayaprakash20</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jayaprakash20"/>
    <language>en</language>
    <item>
      <title>Pointers vs References: A Deep Dive into C++ Fundamentals</title>
      <dc:creator>Jaya Prakash</dc:creator>
      <pubDate>Sat, 19 Jul 2025 06:56:30 +0000</pubDate>
      <link>https://dev.to/jayaprakash20/pointers-vs-references-a-deep-dive-into-c-fundamentals-4024</link>
      <guid>https://dev.to/jayaprakash20/pointers-vs-references-a-deep-dive-into-c-fundamentals-4024</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffp7zbjsj3x6f1zndtt8j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffp7zbjsj3x6f1zndtt8j.png" alt=" " width="800" height="420"&gt;&lt;/a&gt;C++ provides two primary mechanisms for indirectly accessing and modifying data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pointers, which store memory addresses&lt;/li&gt;
&lt;li&gt;References, which act as aliases for existing variables&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Though both can be used to access other variables, they serve different purposes, with distinct syntax, behavior, and safety characteristics.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Pointer?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;pointer&lt;/strong&gt; is a variable that holds the address of another variable.It enables indirect access and manipulation of data stored in a different memory location by referencing its address.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F02654wgbgf42ioq8yqj1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F02654wgbgf42ioq8yqj1.png" alt=" " width="800" height="218"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;int* ptr&lt;/code&gt;: Declares a pointer to an &lt;code&gt;int&lt;/code&gt;. It can store the &lt;strong&gt;address&lt;/strong&gt; of an integer variable.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;amp;x&lt;/code&gt;: The &lt;strong&gt;address-of operator&lt;/strong&gt; (&lt;code&gt;&amp;amp;&lt;/code&gt;) is used to get the memory address of a variable &lt;code&gt;x&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Visualizing a pointer
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl4ibc7ew3ryeg5rrcjto.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl4ibc7ew3ryeg5rrcjto.png" alt=" " width="800" height="630"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Pointer Dereferencing
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ej2dktjy6t3i2th76t3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ej2dktjy6t3i2th76t3.png" alt=" " width="800" height="156"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dereferencing a pointer&lt;/strong&gt; using (&lt;code&gt;*&lt;/code&gt;) gives access to the &lt;strong&gt;actual variable&lt;/strong&gt; it points to, allowing you to read or modify it directly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Null Pointers
&lt;/h3&gt;

&lt;p&gt;Pointers can point to nothing, a null value&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw6yufiu3o1dpao03svs9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw6yufiu3o1dpao03svs9.png" alt=" " width="800" height="217"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Pointer Arithematic
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvv1yd1d1cbwzeiio7jbg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvv1yd1d1cbwzeiio7jbg.png" alt=" " width="800" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;int* ptr = arr;&lt;/code&gt;&lt;br&gt;
This means &lt;code&gt;ptr&lt;/code&gt; points to the first element of the array. In C++, the array name (&lt;code&gt;arr&lt;/code&gt;) automatically acts like a pointer to its first element.&lt;/p&gt;

&lt;h3&gt;
  
  
  Accessing Elements Using Pointer Arithmetic
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwufsup7agh2nx6qpagsz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwufsup7agh2nx6qpagsz.png" alt=" " width="800" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;*ptr&lt;/code&gt; gives the value at the first element.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;*(ptr + 1)&lt;/code&gt; moves the pointer one step ahead and accesses the second element.&lt;/li&gt;
&lt;li&gt;You can keep doing &lt;code&gt;*(ptr + i)&lt;/code&gt; to access &lt;code&gt;arr[i]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Accessing Class Members via Pointer
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbek8f6awmrxpj2lkh51x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbek8f6awmrxpj2lkh51x.png" alt=" " width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ptr-&amp;gt;x&lt;/code&gt; and &lt;code&gt;ptr-&amp;gt;show()&lt;/code&gt; access members through the pointer using the &lt;code&gt;-&amp;gt;&lt;/code&gt; operator.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Reference?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;reference&lt;/strong&gt; is an alias for another variable. Once initialized, it cannot be changed to refer to another object.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcuky62mcf7ejcd8pp08e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcuky62mcf7ejcd8pp08e.png" alt=" " width="800" height="231"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Always initialized during declaration&lt;/li&gt;
&lt;li&gt;No &lt;code&gt;nullptr&lt;/code&gt;, no reassignment&lt;/li&gt;
&lt;li&gt;Looks like a variable, behaves like an alias&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;References are used to avoid copying of data&lt;/strong&gt;, making the function more efficient, especially with large objects.&lt;/li&gt;
&lt;li&gt;They allow functions to &lt;strong&gt;modify the original variable&lt;/strong&gt; directly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv0ae5rc381n0hhk8lk3w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv0ae5rc381n0hhk8lk3w.png" alt=" " width="800" height="672"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pass-by-reference is useful when:

&lt;ul&gt;
&lt;li&gt;You want to avoid performance overhead of copying.&lt;/li&gt;
&lt;li&gt;You need the function to update the caller's variable.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Passing by Reference Without Modification
&lt;/h2&gt;

&lt;p&gt;Using &lt;code&gt;const std::string&amp;amp;&lt;/code&gt; means the function receives the original variable &lt;strong&gt;by reference&lt;/strong&gt;, but &lt;strong&gt;promises not to modify it&lt;/strong&gt;. This ensures safety while avoiding the overhead of copying.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faqsnwzpslmoqx9s04mxl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faqsnwzpslmoqx9s04mxl.png" alt=" " width="800" height="195"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  When to Use Pointers
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Dynamic memory allocation&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Nullable objects&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data structures (linked lists, trees)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interfacing with C libraries&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Arrays and pointer arithmetic&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When to Use References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Function arguments/return types&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Operator overloading&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Avoiding copies&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cleaner syntax for indirection&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Advanced Topics
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Working with Pointers and Custom Classes in C++
&lt;/h3&gt;

&lt;p&gt;In C++, when you create objects using &lt;code&gt;new&lt;/code&gt;, you get a &lt;strong&gt;pointer&lt;/strong&gt; to that object. You must manually &lt;code&gt;delete&lt;/code&gt; it to free memory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1kud4rf991fznnuy658x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1kud4rf991fznnuy658x.png" alt=" " width="800" height="409"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;new&lt;/code&gt; returns a pointer to the heap-allocated object.&lt;/p&gt;

&lt;p&gt;You &lt;strong&gt;must call&lt;/strong&gt; &lt;code&gt;delete&lt;/code&gt; to avoid memory leaks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Returning by Reference
&lt;/h3&gt;

&lt;p&gt;Allows function to return modifiable values:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ferlx1kjm175btqyasu4u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ferlx1kjm175btqyasu4u.png" alt=" " width="800" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Never return a reference to a local variable causes &lt;strong&gt;dangling reference&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example (Dangling Reference):
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzacv7c2l1tqq7pp2mbgu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzacv7c2l1tqq7pp2mbgu.png" alt=" " width="800" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;x&lt;/code&gt; is a &lt;strong&gt;local variable&lt;/strong&gt;, so it gets destroyed when &lt;code&gt;getRef()&lt;/code&gt; ends.&lt;/li&gt;
&lt;li&gt;Returning a reference to it creates a &lt;strong&gt;dangling reference&lt;/strong&gt;, leading to &lt;strong&gt;undefined behavior&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Pointers to Pointers
&lt;/h3&gt;

&lt;p&gt;You can use a pointer to a pointer (&lt;code&gt;int**&lt;/code&gt;) to dynamically create and manage 2D arrays.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwx3s8ethrm9u3aivhpgn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwx3s8ethrm9u3aivhpgn.png" alt=" " width="800" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Smart Pointers (Modern C++)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Smart pointers&lt;/strong&gt; are class templates in C++ that manage the lifetime of dynamically allocated objects. Unlike raw pointers (&lt;code&gt;int*&lt;/code&gt;), they &lt;strong&gt;automatically free memory&lt;/strong&gt; when it's no longer needed, helping prevent:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Memory leaks&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dangling pointers&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Manual delete calls&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They're part of the C++ &lt;strong&gt;Standard Library&lt;/strong&gt; (since C++11) and live in the &lt;code&gt;&amp;lt;memory&amp;gt;&lt;/code&gt; header.&lt;/p&gt;

&lt;h3&gt;
  
  
  Types of Smart Pointers
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;std::unique_ptr&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Owns a resource &lt;strong&gt;exclusively&lt;/strong&gt; (no other pointer can point to it). Automatically deletes the object when it goes out of scope.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkhqr5ft7h23nrxdc46no.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkhqr5ft7h23nrxdc46no.png" alt=" " width="800" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;std::shared_ptr&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Allows &lt;strong&gt;multiple pointers&lt;/strong&gt; to share ownership of a dynamically allocated object. Internally uses &lt;strong&gt;reference counting&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd5gylyrj3uv5j9qp2w5r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd5gylyrj3uv5j9qp2w5r.png" alt=" " width="800" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;std::weak_ptr&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;non-owning&lt;/strong&gt; smart pointer that observes a &lt;code&gt;shared_ptr&lt;/code&gt; without affecting its reference count. Prevents &lt;strong&gt;cyclic references&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgjkgkv8a1tgjj3qt9uxh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgjkgkv8a1tgjj3qt9uxh.png" alt=" " width="800" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;weak_ptr&lt;/code&gt; &lt;strong&gt;does not own the object&lt;/strong&gt;, so it can’t access it directly.To use the object safely, you use the &lt;code&gt;.lock()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.lock()&lt;/code&gt; does two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Checks if the object still exists (hasn't been deleted).&lt;/li&gt;
&lt;li&gt;Returns a &lt;code&gt;shared_ptr&lt;/code&gt; to access the object &lt;strong&gt;only if it's still alive&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What is Reference Count in Smart Pointers?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Reference count&lt;/strong&gt; is a number that keeps track of &lt;strong&gt;how many&lt;/strong&gt; &lt;code&gt;shared_ptrs&lt;/code&gt; are &lt;strong&gt;pointing to the same object&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This count:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Increases&lt;/strong&gt; when a &lt;code&gt;new&lt;/code&gt; &lt;code&gt;shared_ptr&lt;/code&gt; is created from an existing one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decreases&lt;/strong&gt; when a &lt;code&gt;shared_ptr&lt;/code&gt; is destroyed or reset.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When the reference &lt;strong&gt;count drops to zero&lt;/strong&gt;, it means &lt;strong&gt;no one is using the object anymore&lt;/strong&gt;, so it gets &lt;strong&gt;automatically deleted&lt;/strong&gt; — no manual &lt;code&gt;delete()&lt;/code&gt; needed&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyv4jvcwe0rsgii255652.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyv4jvcwe0rsgii255652.png" alt=" " width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>developer</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
