Dynamic Memory Allocation in C Using malloc()

Dynamic Memory Allocation in C Using malloc()

·

3 min read

C is a very old language, with its existence dating back to 1970s. It has been the most influential language in the world with pretty much everything around us. Our Operating systems, routers and many other devices run using C.

Despite being that influential, C is bare-bones and lacks a lot of features that modern programming languages offer. The major one being lack of automatic memory allocation.

Which means, we have to allocate and de-allocate memory manually in C for dynamic data structures which means there's more room for error and not knowing how to do it may crash your program.

While C doesn't allow automatic memory allocation, it does have some built-in methods for us to use for dynamic memory allocation.

What is Dynamic Memory Allocation?

In modern languages like Java and Python, a variable gets de-allocated from the memory automatically by the interpreter when it goes out of scope, this is nothing but automatic memory management.

In case of C, which is a compiled language, it entirely lacks this feature. Which means that we have to manually allocate and de-allocate memory for the said variable.

While this might sound like a disadvantage, it's actually beneficial in languages like C because you can change the memory size of a variable at runtime rather than predefining the size of the array or variable.

How to use it?

C has built-in functions for dynamic memory allocation in a header file named stdlib.h.

stdlib.h contains the following functions for DMA:

  • malloc()

  • calloc()

  • realloc()

  • free()

We will be focusing on malloc() and free() in this blog post.

malloc()

It is a built-in function in stdlib.h which allocates a single, large block of memory to the given variable of specified size.

Syntax

ptr = (cast-type*) malloc(byte-size);

free()

We need to de-allocate the memory that we have allocated once we are done using it and we use free() for this. free() de-allocates all of the memory assigned to that variable.

Syntax

free(ptr_name);

Implementation

What's the use of learning something if we don't apply it? So let us apply the DMA knowledge we learnt in a C program.

Here we will be creating and displaying elements of an array using DMA.

Coding Time

First, we need to include the necessary header files which are stdio.h and stdlib.h.

#include<stdio.h>
#include<stdlib.h>

Followed by the main function.

int main(){
}

Now we need to initialise a few variables. One for taking in the input of size of the array and a pointer variable for the array.

int main(){
    int *parr,n,i;
    printf("Enter the size of the array");
    scanf("%d",&n);
}

Now we need to dynamically allocate the memory for this array, for this , we use malloc().

 parr=(int*)malloc(n*sizeof(int));

Now we can just proceed with our regular reading array and printing array operations but with minor modifications.

printf("Enter the array elements: \n");
for(i=0;i<n;i++){
   scanf("%d",parr+i);
}
printf("The entered array elements are: ");
for(i=0;i<n;i++){
   printf("%d ",*(parr+i));
}

Notice how we are using and assigning the values and calling them in the scanf() and printf(). That's one method to approach malloc().

Finally, we have to free the allocated memory using free().

free(parr);

Full Code

#include<stdio.h>
#include <stdlib.h>
int main(){
    int *parr,m,n,i;
    printf("Enter no of elements in array: ");
    scanf("%d",&n);
    parr=(int*)malloc(n*sizeof(int));
    printf("Enter the array elements: \n");
    for(i=0;i<n;i++){
        scanf("%d",parr+i);
    }
    printf("The entered array elements are: ");
    for(i=0;i<n;i++){
        printf("%d ",*(parr+i));
    }
    free(parr);
}

Conclusion

That is how you implement and use dynamic memory allocation using malloc() in C.