C Programming: Understanding Dynamic Memory Allocation
overview
As a structured language, C follows certain prescribed rules for programming. One such rule pertains to altering the size of an array, which is a group of elements that are stored in adjacent memory locations.
what is dynamic memory allocation
Dynamic Memory Allocation is a technique that allows the modification of the size of a data structure, such as an array, during runtime.
C programming provides several functions to accomplish this task, and four such library functions are available in the <stdlib.h> header file. These functions include: malloc(), calloc(), free(), and realloc().
why the need for dynamic memory allocation arises
Consider an array comprising 10 elements, each of which is occupied. In one scenario, the number of elements may decrease, leaving certain indices of the array unused, thereby leading to memory wastage. Alternatively, in another scenario, the number of elements may increase beyond the array's size, necessitating the expansion of the array. In either case, dynamic memory allocation can be employed as a solution to address the issue.
Now, let's dive deeper into each of these functions to gain a better understanding of how they operate.
Malloc()
It is a method in C that is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form. It doesn’t Initialize memory at execution time so it has initialized each block with the default garbage value initially.
// SYNTAX:
ptr = (cast-type*) malloc(byte-size);
Calloc()
It is a method in C that is used to dynamically allocate the specified number of blocks of memory of the specified type. it is very much similar to malloc() but has two different points and these are:
-> It initializes each block with a default value ‘ 0 ’.
-> It has two parameters or arguments as compared to malloc().
// SYNTAX
ptr = (cast-type*)calloc(n, element-size);
Realloc()
It is a method in C that is used to dynamically change the memory allocation of a previously allocated memory. In other words, if the memory previously allocated with the help of malloc or calloc is insufficient, realloc can be used to dynamically re-allocate memory. re-allocation of memory maintains the already present value and new blocks will be initialized with the default garbage value.
// SYNTAX:
ptr = realloc(ptr, newSize);
Free()
It is a method in C that is used to dynamically de-allocate the memory. The memory allocated using functions malloc() and calloc() is not de-allocated on its own. Hence the free() method is used, whenever the dynamic memory allocation takes place. It helps to reduce the wastage of memory by freeing it.
// SYNTAX:
free(ptr);
Conclusion
dynamic memory allocation is a powerful technique that allows programmers to modify the size of a data structure during runtime, thereby reducing memory wastage and improving the efficiency of their programs. The C programming language provides several functions for dynamic memory allocation, including malloc(), calloc(), realloc(), and free(). These functions allow programmers to allocate, reallocate, and deallocate memory as needed, depending on the specific requirements of their programs. By understanding how these functions work and when to use them, programmers can write more efficient and effective code.
Until next time, keep learning!
Subscribe to my newsletter
Read articles from Abhinav Rana directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Abhinav Rana
Abhinav Rana
Hey there! I'm a passionate student on a mission to unravel the wonders of the tech universe. With a knack for logical thinking, problem-solving, and lightning-fast learning. As I code my way towards the future, I'm excited to pave the path to DevOps, where I'll merge my love for development and operations.