Dynamic Memory Allocation in C
Tags: computerscience, c, programming
Published: 2021-05-08 02:01:05

In this article, I will explain how to dynamically allocate memory in C. 

*malloc()*
<ul>
<li>This function allows us to dynamically allocate memory.</li>
<li> This function comes from the standard library. </li>
<li> You specify the number of bytes of memory you want to allocate as the argument. </li>
<li> It returns the address of the first byte of memory that is allocated. </li>
<li>Because you get an address returned, you must pass in a pointer.</li>
<li> Always call free() after done using malloc(). </li>
<li> Syntax -> *ptr = (cast-type*) malloc(byte-size)* </li>
</ul>


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/goabaryqaotoy3fa48ah.png)

<ul>
<li> You always want to check to see if you can allocate memory. If your computer is not able to, that is a major problem and you need to exit or abort from the program. </li>
<li> In this program, we are asking the user the total amount of numbers. This will allocate that many integers. </li>
<li> After that, we check to see if our pointer is pointing to NULL. If it is, then we abort from the program. This means that memory could not be allocated. </li>
<li> After that, we make a for loop, asking the user to input each number. </li>
<li> Then, we do another for loop to print each number to the console. </li>
<li> Finally, we call free() to free up the memory. This is because we are no longer needing it. This will prevent a memory leak. </li>
</li> When we dynamically allocate memory, it is our responsibility to manage that memory. If we don't, we will run into memory leaks. </li>
</ul>

<hr/>

*calloc()*
<ul>
<li>This function is very similar to malloc()</li>
<li>It creates contiguous blocks of memory, where malloc() creates only one. </li>
<li>It is slower than malloc()</li>
<li>It initializes the allocated memory blocks to zero, where malloc() does not. </li>
<li> It takes in two arguments : number of data items, and size of each data item. </li>
<li> Syntax -> *ptr = (cast-type*)calloc(num of elements, byte-size)*</li>
</ul>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fo4agxhl8ix3afdmbmkw.png)

<ul>
<li>This is very similar to the previous program, except instead of malloc(), we called calloc(). </li>
<li> We set each data item with a initial value of zero. </li>
<li> If we tried to access the value with malloc(), we would get a segmentation fault error. However, with calloc(), we would get 0.</li>
</ul>

<hr/>

*realloc()*
<ul>
<li> This function enables you to reuse/extend memory that you previously allocated with malloc() or calloc(). </li>
<li> Expects 2 arguments: <ul><li> a pointer containing an address that was previously returned by a call to malloc or calloc. <li>Size in bytes of new memory you want allocated. </li></ul></li>
<li> Transfers the contents of the previously allocated memory to the new allocated memory. </li>
<li> This function deallocates the old object and returns a pointer to a new object. </li>
</ul>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yhojvgbk6n1s0ygfdt7i.png)

<ul> 
<li>This is a simple program to demonstrate how realloc() works. </li>
<li>We create a char pointer that holds 15 characters. Then, we copy "tommy" to that string. </li>
<li> Then, we print the string and memory address of it. </li>
<li> After that, we reallocate more memory. Now, instead of holding 15 characters, now we are holding 25 characters. </li>
<li> We concatenate "kraljic" to the other string and print the contents of the string and the memory address. </li>
<li> You will notice, that there are two different memory addresses. This is because, we initially had the string in one memory address. However, when we called realloc(), we deallocated the old memory and allocated memory in a new address. </li>
<li> Finally, we call free to free up the memory so we don't have a memory leak. </li>
</ul>  

<hr/>

*Guidelines*
<ul>
<li> Always, always, always call free() when you are done with the memory. We do not want memory leaks.</li>
<li> Avoid allocating lots of small amount of memory. </li>
<li> Only dynamically allocate memory if you need to. </li>
<li> Allocating many small blocks of memory will much more overhead than allocating fewer larger blocks. </li>
</ul>