<?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: Owais Orakzai</title>
    <description>The latest articles on DEV Community by Owais Orakzai (@owais_orakzai).</description>
    <link>https://dev.to/owais_orakzai</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%2F763418%2F3fdd7adc-8a3d-4229-9368-1d01b1fb767d.png</url>
      <title>DEV Community: Owais Orakzai</title>
      <link>https://dev.to/owais_orakzai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/owais_orakzai"/>
    <language>en</language>
    <item>
      <title>Memory management in depth. Lets discuss stacks and heaps</title>
      <dc:creator>Owais Orakzai</dc:creator>
      <pubDate>Sun, 05 Dec 2021 00:57:19 +0000</pubDate>
      <link>https://dev.to/owais_orakzai/memory-management-in-depth-lets-discuss-stacks-and-heaps-59oh</link>
      <guid>https://dev.to/owais_orakzai/memory-management-in-depth-lets-discuss-stacks-and-heaps-59oh</guid>
      <description>&lt;p&gt;Memory management is process of controlling, allocating and using computer memory to optimize overall performance of system. Now this may sound a bit foggy but don't worry we will discuss it one by one.&lt;/p&gt;

&lt;p&gt;Each time a program runs on your computer, a memory block is allocated to it by your Operating System. We call this memory block &lt;strong&gt;a segment&lt;/strong&gt;. Each segment is then further divided into smaller blocks which is used to store chunks of data. Below visual representation of memory segment&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e0hN50vU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3vie719hgnj1hfx3zvif.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e0hN50vU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3vie719hgnj1hfx3zvif.png" alt="Visual Representation of Memory Segment" width="200" height="200"&gt;&lt;/a&gt;&lt;br&gt;
Lets just for now assume that we store some data such as 'Hello' and 'World'. And after sometime we decide to use convert 'Hello' to French and want to change it to something like 'Bonjour' (&lt;em&gt;Btw I ain't French but this is what came into my mind at this time xD&lt;/em&gt;). But the question how will the computer know that 'Hello' is stored in block 1?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K-J85Ud_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/472tlwhklac9o78fwqes.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K-J85Ud_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/472tlwhklac9o78fwqes.png" alt="" width="200" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well that's where addresses comes in. In your memory segment, each block has a separate and unique address. Computer uses that address to read/write/update value stored in that block. Each time a variable is declared in your program, computer assigns a memory block to it in stack or heap depending upon the request made by user. BUT what is this stack and heap? Lets find out together.&lt;/p&gt;
&lt;h1&gt;
  
  
  Stack vs Heap
&lt;/h1&gt;

&lt;p&gt;Remember memory segment? Well turns out that your memory segment is divided into three main parts&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Stack&lt;/li&gt;
&lt;li&gt;Heap&lt;/li&gt;
&lt;li&gt;Code Block&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rNCwK7ev--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/to396yrjshaum3nup4xf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rNCwK7ev--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/to396yrjshaum3nup4xf.png" alt="Memory Segment Parts" width="200" height="200"&gt;&lt;/a&gt;&lt;br&gt;
Just as discussed above that whenever variable is declared, a memory block is assigned to it but that essentially happens in the stack portion of memory segment. In general stack is nothing but a portion of memory segment which is reserved to hold data such as data stored in variables and data pushed into stack. And so is heap too. But the difference is that heap should be used as a resource. The resource here means that after we are done using memory in heap, it should be deallocated so that other program can access and use it.&lt;/p&gt;

&lt;p&gt;Now lets go a bit deeper and discuss these thing more technically. Lets just say you declare a function call &lt;code&gt;myFunc&lt;/code&gt;. And you have declared some variables in it such as &lt;code&gt;int a,b&lt;/code&gt;. So your function will look something like this.&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;void&lt;/span&gt; &lt;span class="nf"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After you declare your function like this, this is how your memory segment look&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zlPwHpVa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpjmvdxt0zbmbt4ock03.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zlPwHpVa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpjmvdxt0zbmbt4ock03.png" alt="Memory Allocation Stack" width="200" height="200"&gt;&lt;/a&gt;&lt;br&gt;
Note here that memory for &lt;code&gt;int a,b;&lt;/code&gt; is a allocated here in stack portion. After function is executed,memory assigned to these variables will also be free automatically. But what if we want to allocate memory in heap? Well for that we have something we call pointers. And you can declare pointer like &lt;code&gt;int *ptr&lt;/code&gt;. We use pointers because memory in heap is not directly accessible to us. We initialize pointer which in returns us an address of memory block in heap and using that address we perform operations on that memory block. BUT please note one important point here that pointer itself is actually stored on stack while the memory it points actually resides on heap&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OI13N8nZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gj4l0mgwkvcuiakl1391.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OI13N8nZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gj4l0mgwkvcuiakl1391.png" alt="Memory Allocation Heap" width="200" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's how you can initialize a pointer&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;ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;BUT there is a catch. In stacks memory is deallocated automatically after we go out of scope of variable but that is not the case in heap. Heap memory should be deallocated explicitly. And that is where the keyword &lt;code&gt;delete&lt;/code&gt; comes in.&lt;/p&gt;

&lt;p&gt;Here's how its done&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="k"&gt;delete&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What it will do is delete everything stored on address in ptr. And assign &lt;code&gt;NULL&lt;/code&gt; to ptr.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;That's it. This is it for today. I hope you like this article and if you do don't forget to subscribe&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>beginners</category>
      <category>programming</category>
      <category>csharp</category>
    </item>
  </channel>
</rss>
