09
NovHow to Dynamically Allocate Memory using malloc() in C?
malloc() Function in C
malloc() Function in C plays an important part in the process of Dynamic Memory Allocation. It is defined in the <stdlib.h> header file. The function allocates particular size bytes of memory and returns the pointer pointing towards the allocated memory.
In this C Tutorial, we'll learn more about Dynamic Memory Allocation, defining malloc() in C Programming with an Example Program, etc. For more understanding of C Programming, consider enrolling in our C Certification Training.
Read More: Top 50 Mostly Asked C Interview Questions and Answers
What is Dynamic Memory Allocation?
Dynamic Memory Allocation in C is the process that allows the developer to dynamically allocate memory during the runtime. That means the size and allocation of the data can be changed if required during the runtime with the help of the method of Dynamic Memory Allocation. Four kinds of functions help in achieving this:
- malloc()
- calloc()
- realloc()
- free()
Let's get to know the malloc() function and its application in detail first.
The malloc() in C Programming
The malloc(), which stands for 'memory allocation', is a function for performing memory allocation by allocating the memory in the form of blocks of sizes that are specified. And then, it returns a pointer of void to the starting of that memory block.
Read More: Pointers in C Programming
malloc() syntax in C:
void* malloc(size_t size);
The 'size' keyword is used to specify what number of bytes are needed to be allocated.
Read More: Beginner's Guide to C Programming
malloc() Function in C library with EXAMPLE
We already know what malloc() function does. Now, we will take a look at how to use malloc() in C Program through the below Example in C Compiler.
Example
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n = 5; // Number of integers to allocate
// Allocate memory for n integers
ptr = (int*)malloc(n * sizeof(int));
// Check if memory allocation is successful
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Access and manipulate the allocated memory
for (int i = 0; i < n; i++) {
ptr[i] = i * 10;
}
// Display the allocated memory contents
printf("Allocated memory contents:\n");
for (int i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}
// Free the allocated memory
free(ptr);
return 0;
}
Explanation
Output
Allocated memory contents:
0 10 20 30 40
Advantages of malloc() in C Programming
- Firstly, the biggest advantage is that it allows the developer to allocate memory dynamically when the program is ready for execution
- malloc() function helps in efficiently managing memory as we are allocating memory only and only when it is needed so it enables us to optimize memory as well.
- Sometimes, we are working on large arrays and it is difficult to fit in the stack memory as it is limited but with the help of malloc() allocation of large data structures is also possible.
- Using malloc() function, we can also implement dynamic data structures such as linked lists, trees, graphs, etc.
- It also offers error handling mechanisms to detect errors and failures in the early stages and fix them as soon as possible.
- Through malloc(), memory is allocated dynamically that can be reused for different purposes as well within the program.
- It is portable too as it is a C library function that is suitable to work on different C compilers and different platforms.
Disadvantages of malloc() in C Programming
- All the memory allocation need manual efforts to manage the allocation properly with deallocation by using free() too.
- Sometimes, there are cases where the memory can be fragmented into small blocks of memory over time that can be a problem in future memory allocation.
- There are chances of memory leaks if we, somehow, do not use the free() function to deallocate the allocated memory.
- It is a dynamic function which also makes it error prone.
- Memory can be corrupted in some cases where pointers are not correctly used.
Difference Between Static and Dynamic Memory Allocation
Static Memory Allocation | Dynamic Memory Allocation |
Static memory allocation occurs at compile time. | Dynamic memory allocation occurs at runtime. |
It cannot be deallocated during runtime and remains available throughout. | It is deallocated using the free() function. |
Allocated on the stack or in the global data segment. | Allocated from the heap. |
Flexibility is lower. | It is much more flexible as compared to the other. |
For example- global variables, static variables. | For example- memory is allocated with the help of malloc(), calloc(), and realloc() functions. |
Summary
FAQs
void* malloc(size_t size);