<?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: Abhiru Karki</title>
    <description>The latest articles on DEV Community by Abhiru Karki (@abhiruuu).</description>
    <link>https://dev.to/abhiruuu</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%2F1362288%2Fd4661464-18da-4588-8ff3-3b8eb4a67eb6.png</url>
      <title>DEV Community: Abhiru Karki</title>
      <link>https://dev.to/abhiruuu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abhiruuu"/>
    <language>en</language>
    <item>
      <title>How do Dynamic Arrays actually work ?</title>
      <dc:creator>Abhiru Karki</dc:creator>
      <pubDate>Wed, 25 Dec 2024 16:28:36 +0000</pubDate>
      <link>https://dev.to/abhiruuu/growing-pains-how-dynamic-arrays-handle-new-elements--36m7</link>
      <guid>https://dev.to/abhiruuu/growing-pains-how-dynamic-arrays-handle-new-elements--36m7</guid>
      <description>&lt;p&gt;Dynamic Arrays might seem simple at first glance, but their inner workings will open your eyes 👀.&lt;/p&gt;

&lt;p&gt;But first of all, let's start with the basics.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Dynamic Arrays ?
&lt;/h2&gt;

&lt;p&gt;Dynamic Arrays are resizable arrays that can automatically adjust their size when elements are added ➕ or removed ➖.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Unlike &lt;strong&gt;&lt;em&gt;Static Arrays&lt;/em&gt;&lt;/strong&gt; which has a &lt;strong&gt;fixed size&lt;/strong&gt; that is determined during compile time, the size of Dynamic Arrays can be adjusted during &lt;em&gt;run time&lt;/em&gt; as per the need. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  But are Dynamic Arrays really Dynamic ?
&lt;/h2&gt;

&lt;p&gt;Actually Dynamic Arrays are built &lt;strong&gt;on top of Static Arrays&lt;/strong&gt;. Surprising right ? &lt;strong&gt;Let's dive deeper 🤿&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Working Mechanism ⚒️
&lt;/h2&gt;

&lt;p&gt;Suppose we have a Dynamic Array &lt;code&gt;myArr[3] = {2, 4, 6}&lt;/code&gt; which is already full.&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%2F0l9hl7rqggldsfi88icr.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%2F0l9hl7rqggldsfi88icr.png" alt="Image description" width="661" height="548"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, If you want to add a new element to the array according to Dynamic Array, you may think that a new slot will be added to the existing array and the element is then after inserted into it. What if I tell you "YOU ARE WRONG" ? 🤨&lt;/p&gt;

&lt;h3&gt;
  
  
  The Moment of Truth ✅
&lt;/h3&gt;

&lt;p&gt;What actually happens is that a new array is created with &lt;strong&gt;double or greater capacity&lt;/strong&gt; than the existing array ( no worries, will explain the reason for this too ) , and all of the existing elements are shifted to the new array, as well as new elements, and then the pointer head 🫵 is shifted from the previous array to the new array 🫵, and the previous array is deallocated &lt;strong&gt;if necessary&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%2F529ekfr2lvp1c807byfa.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%2F529ekfr2lvp1c807byfa.png" alt="Image description" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Now why the new array has double or much greater capacity than the previous array ? 🤨
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;It's simple. &lt;br&gt;
So that we don't run out of space frequently, which would require us to create a new array every time we add a single new element, increasing the time complexity of the process. Instead, why not just create a little larger but not too large new array to balance the time and space complexity ? &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;So yeah that was it 🤷&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now you know how Dynamic Arrays actually work. Understanding the mechanics of dynamic arrays is crucial for efficient programming. &lt;/p&gt;

&lt;p&gt;As you continue your coding journey, keep these principles in mind to optimize your data handling strategies. Happy Coding!&lt;/p&gt;

</description>
      <category>datastructures</category>
      <category>programming</category>
      <category>arrays</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
