<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sarah Hassan on Medium]]></title>
        <description><![CDATA[Stories by Sarah Hassan on Medium]]></description>
        <link>https://medium.com/@sarrahhassan?source=rss-a61f81ca3de4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*H832vISTwN0VRYaDe1oLlw.jpeg</url>
            <title>Stories by Sarah Hassan on Medium</title>
            <link>https://medium.com/@sarrahhassan?source=rss-a61f81ca3de4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 00:04:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sarrahhassan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Dynamic Memory Allocation in C ‍♂️]]></title>
            <link>https://sarrahhassan.medium.com/dynamic-memory-allocation-in-c-%EF%B8%8F-9693f07b31fe?source=rss-a61f81ca3de4------2</link>
            <guid isPermaLink="false">https://medium.com/p/9693f07b31fe</guid>
            <dc:creator><![CDATA[Sarah Hassan]]></dc:creator>
            <pubDate>Sun, 09 Jul 2023 19:24:51 GMT</pubDate>
            <atom:updated>2023-07-29T00:10:27.387Z</atom:updated>
            <content:encoded><![CDATA[<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F7nUmu1XgcNw%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D7nUmu1XgcNw&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F7nUmu1XgcNw%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/9eea92da8f7df687597f05e5cfbc1fa5/href">https://medium.com/media/9eea92da8f7df687597f05e5cfbc1fa5/href</a></iframe><h3>Party Time 🥳🎉</h3><p>Imagine you’re hosting your birthday party at a luxury place, and you have extended invitations to all your friends. However, only 50 of them have confirmed their attendance. It poses a significant challenge as you want to ensure there is enough space for everyone, but you still cannot accurately predict the exact number of guests who will actually attend. Such a huge dilemma, isn’t it? 🤕🤒</p><h3>Dynamic Party Allocation ???!! 🦸‍♂️</h3><p>ٍSo the party man suggested that you can take the Dynamic Party Allocation package, which allows you to reserve a fixed number of people whose attendance is confirmed, and then expand the reservation as the number increases.</p><p>You took the package and solved the problem, Now you can easily celebrate without the fear of not finding a place for all your guests. Thanks, Party man! YOU SAVED MY LIFُE!🦸‍♂️</p><h3>What Is The Dynamic Memory Allocation?</h3><p>typically when you have an array “ <em>party</em>” with a fixed size but you’re not sure if your fixed size array would be enough for all your future elements “ <em>guests</em>” here’s where <strong><em>Dynamic Memory Allocation </em></strong>comes into play. 🏃‍⛹️‍♂️</p><p><strong><em>Dynamic memory allocation</em></strong> is a powerful feature in the C programming language that allows programmers to manage and allocate memory based on the actual needs of your program at runtime. Unlike <strong><em>Static Memory Allocation</em></strong>, where the size is fixed and determined during compilation.</p><p><strong><em>Dynamic Memory Allocation</em></strong> gives you the flexibility to adjust the memory requirements during program execution so you can manage your resources and reuse the memory as much as you need then free it when you’re done!!</p><p><strong>In this article, we will delve into the world of dynamic memory allocation in C, exploring its concepts, functions, and best practices.</strong></p><h3>How To Use Dynamic Memory Allocation In C?</h3><p>C language provides you with a set of functions “<a href="https://www.tutorialspoint.com/c_standard_library/c_function_malloc.htm"><strong><em>malloc</em></strong></a><strong><em>(), </em></strong><a href="https://www.tutorialspoint.com/c_standard_library/c_function_realloc.htm"><strong><em>realloc</em></strong></a><strong><em>(), </em></strong><a href="https://www.tutorialspoint.com/c_standard_library/c_function_calloc.htm"><strong><em>calloc</em></strong></a><strong><em>() , </em></strong><a href="https://www.tutorialspoint.com/c_standard_library/c_function_free.htm"><strong><em>free</em></strong></a><strong><em>()“</em></strong> which you can use to allocate and deallocate memory in your program</p><p>Use malloc(), realloc(), calloc()<strong><em> </em></strong>to reserve blocks of memory and access these chunks of memory whenever you need using their pointers. Once you’re done use free()with the pointer to get rid of the allocated memory.</p><pre>ptr = (data_type *)malloc(size);<br>free(ptr);</pre><h3>Back to our birthday party 🎉🥳</h3><p>Now let&#39;s transform our lovely party into code and apply <strong><em>Dynamic Party Allocation 🦸‍♂️</em></strong></p><pre>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>int main() {<br>    int fixedSize = 10; // Fixed size of the array<br>    int* party = (int*) malloc(fixedSize * sizeof(int));<br>    int guests;<br>    <br>    if (party == NULL) {<br>        printf(&quot;Memory allocation failed. Exiting...\n&quot;);<br>        return 1;<br>    }<br>    <br>    printf(&quot;Enter the number of guests: &quot;);<br>    scanf(&quot;%d&quot;, &amp;guests);<br>    <br>    // Check if the number of guests exceeds the fixed size<br>    if (guests &gt; fixedSize) {<br>        int newSize = guests; // Determine the new size<br>        int* temp = (int*) realloc(party, newSize * sizeof(int));<br>        <br>        if (temp == NULL) {<br>            printf(&quot;Memory reallocation failed. Exiting...\n&quot;);<br>            free(party); // Free the original memory<br>            return 1;<br>        }<br>        <br>        party = temp; // Assign the new memory location<br>        fixedSize = newSize; // Update the fixed size<br>    }<br>    <br>    printf(&quot;Enter the IDs of the guests:\n&quot;);<br>    for (int i = 0; i &lt; guests; i++) {<br>        scanf(&quot;%d&quot;, &amp;party[i]);<br>    }<br>    <br>    printf(&quot;Guest IDs:\n&quot;);<br>    for (int i = 0; i &lt; guests; i++) {<br>        printf(&quot;%d\n&quot;, party[i]);<br>    }<br>    <br>    free(party);<br>    <br>    return 0;<br>}<br></pre><p><strong>Now obviously you can extend your party capacity whenever the guests number increases!</strong></p><h3><strong>A Good Practice ✔🐱‍🏍</strong></h3><p>Pay attention to free(), as it is important to always deallocate and free memory that is no longer used. free() allows the operating system to reclaim the memory and make it available for other parts of the program or other programs running on the system.</p><p>Failing to free allocated memory can lead to memory leaks and memory errors, which are not good things to have. Memory leaks occur when memory is allocated but not released, causing the program to consume increasing amounts of memory over time.</p><p>You can read this article on Medium for more information about memory leaks and errors =&gt; <a href="https://medium.com/@emadanwer.official/memory-leak-and-the-delivery-guy-%EF%B8%8F-3678e402cfa0"><strong><em>Memory leak and the delivery 🛵guy 👨‍⚕️</em></strong></a></p><p><strong>And that&#39;s all for <em>Dynamic Memory Allocation in C, </em>I hope it was worth reading and helpful to you. If you have any further questions or need clarification please feel free to ask. Thank you for your time!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9693f07b31fe" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>