Introduction
In this article, we will discuss dynamic memory allocation in C programming language, its importance, and how to use it effectively.
Also Read: Program To Reverse a String in C using Pointer
Dynamic memory allocation in c programming is an essential concept that enables developers to allocate memory at runtime. It allows programs to use memory more efficiently and effectively by allocating memory only when it is needed and releasing it when it is no longer required.
What is Dynamic Memory Allocation in C Programming?
In C programming, memory can be allocated in two ways: static memory allocation and dynamic memory allocation.
Static memory allocation is done during the compile-time and allocated memory remains fixed throughout the program execution.
However, dynamic memory allocation in c programming is done during the runtime and allocated memory can be changed according to program requirements.
Dynamic Memory Allocation Functions
Dynamic memory allocation is done using special functions called memory allocation functions. In C programming language, there are four memory allocation functions:
- malloc()
- calloc()
- realloc()
- free()
Malloc Function
The malloc function is used to allocate memory dynamically at runtime. It takes the size of the memory to be allocated in bytes as its parameter and returns a void pointer to the starting address of the allocated memory block.
Here is an example of using the malloc function in C programming:
int ptr;
ptr = (int) malloc(100 * sizeof(int));
In the above example, we have allocated memory dynamically for an array of 100 integers using the malloc function.
The sizeof operator returns the size of the integer data type in bytes, and we have multiplied it by 100 to allocate memory for an array of 100 integers.
The malloc function returns a void pointer, so we have typecasted it to an integer pointer before assigning it to the ptr variable.
// Program to dynamically allocate memory for an integer variable #include <stdio.h> #include <stdlib.h> int main() { int num; num = (int) malloc(sizeof(int)); if(num == NULL) { printf("Memory allocation failed!"); return 1; } *num = 10; printf("The value of num is: %d", *num); free(num); return 0; }
Calloc Function
We can also use this calloc function to allocate memory dynamically at runtime. It takes two parameters, the number of elements for the allocation and the size of each element in bytes.
It initializes the allocated memory to zero. Here is an example of using the calloc function in C programming:
int ptr;
ptr = (int) calloc(100, sizeof(int));
In the above example, we have allocated memory dynamically for an array of 100 integers using the calloc function.
The calloc function initializes the allocated memory to zero, so we do not need to explicitly initialize it.
// Program to dynamically allocate memory for an integer array #include <stdio.h> #include <stdlib.h> int main() { int num; num = (int) calloc(5, sizeof(int)); if(num == NULL) { printf("Memory allocation failed!"); return 1; } num[0] = 10; num[1] = 20; num[2] = 30; num[3] = 40; num[4] = 50; for(int i = 0; i < 5; i++) { printf("The value of num[%d] is: %d\n", i, num[i]); } free(num); return 0; }
Realloc Function
We can use this realloc function to reallocate memory dynamically at runtime. It takes two parameters, a pointer to the previously allocated memory block and the new size of the memory block in bytes.
Here is an example of using the realloc function in C programming:
int ptr;
ptr = (int) malloc(100 * sizeof(int));
ptr = (int*) realloc(ptr, 200 * sizeof(int));
In the above example, we have first allocated memory dynamically for an array of 100 integers using the malloc function.
We have then reallocated the memory block to increase its size to 200 integers using the realloc function. The realloc function returns a void pointer, so we have typecasted it to an integer pointer before assigning it to the ptr variable.
// Program to dynamically reallocate memory for an integer array #include <stdio.h> #include <stdlib.h> int main() { int num; num = (int) malloc(5 * sizeof(int)); if(num == NULL) { printf("Memory allocation failed!"); return 1; } num[0] = 10; num[1] = 20; num[2] = 30; num[3] = 40; num[4] = 50; num = (int*) realloc(num, 10 * sizeof(int)); if(num == NULL) { printf("Memory reallocation failed!"); return 1; } num[5] = 60; num[6] = 70; num[7] = 80; num[8] = 90; num[9] = 100; for(int i = 0; i < 10; i++) { printf("The value of num[%d] is: %d\n", i, num[i]); } free(num); return 0; }
Free Function
The free function is used to deallocate memory that was previously allocated dynamically using malloc, calloc, or realloc function.
Here is an example of using the free function in C programming:
int ptr;
ptr = (int) malloc(100 * sizeof(int));
// Use the allocated memory block
free(ptr);
In the above example, we have first allocated memory dynamically for an array of 100 integers using the malloc function.
We have then used the allocated memory block and finally deallocated the memory using the free function. After deallocation, the memory block becomes available for reuse.
Importance of Dynamic Memory Allocation
Dynamic memory allocation is important because it allows programs to use memory more efficiently and effectively. When we need or release a memory, it allows programs to allocate and deallocate.
Some Important C Programs
- Program in C to Find Longest Line in a File
- Palindrome in C using Pointers
- Insert and Delete element in Array in C using switch case
- C Program to Add Alternate Elements of a 2D Array
- Arrays in C for Complete Beginners
- C Program to Find Area of a Circle using Preprocessor
- Program in C to Remove White Spaces and Comments from a File
- C Program to Print Numbers Except Multiples of n
- Reverse a Number using getchar and putchar function in c
- The while loop in C Programming