Dynamic Memory Allocation in C
Dynamic Memory Allocation in C
Search... Sign In
C Tutorial Interview Questions Examples Quizzes Projects Cheatsheet File Handling Multithreading Memory Layout DSA in C
Dynamic memory allocation techniques give programmer control of memory when to allocate, how much to allocate and
when to de-allocate.
Dynamic memory allocation is possible in C by using the following 4 library functions provided by <stdlib.h> library:
malloc( )
The malloc( ) (stands for memory allocation) function is used to allocate a single block of contiguous memory on the heap at
runtime. The memory allocated by malloc( ) is uninitialized, meaning it contains garbage values.
Assume that we want to create an array to store 5 integers. Since the size of int is 4 bytes, we need 5 * 4 bytes = 20 bytes of
memory. This can be done as shown:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(20);
Output
1 2 3 4 5
In the above malloc call, we hardcoded the number of bytes we need to store 5 integers. But we know that the size of the
integer in C depends on the architecture. So, it is better to use the sizeof operator to find the size of type you want to store.
[Link] 1/9
11/6/25, 1:42 AM Dynamic Memory Allocation in C - GeeksforGeeks
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int) * 5);
Output
1 2 3 4 5
Moreover, if there is no memory available, the malloc will fail and return NULL. So, it is recommended to check for failure by
comparing the ptr to NULL.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int) * 5);
Output
1 2 3 4 5
[Link] 2/9
11/6/25, 1:42 AM Dynamic Memory Allocation in C - GeeksforGeeks
Syntax
malloc(size);
This function returns a void pointer to the allocated memory that needs to be converted to the pointer of required type to be
usable. If allocation fails, it returns NULL pointer.
calloc( )
The calloc( ) (stands for contiguous allocation) function is similar to malloc( ), but it initializes the allocated memory to zero. It
is used when you need memory with default zero values.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)calloc(5, sizeof(int));
Output
0 0 0 0 0
[Link] 3/9
11/6/25, 1:42 AM Dynamic Memory Allocation in C - GeeksforGeeks
Syntax
calloc(n, size);
where n is the number of elements and size is the size of each element in bytes.
This function also returns a void pointer to the allocated memory that is converted to the pointer of required type to be usable.
If allocation fails, it returns NULL pointer.
free( )
The memory allocated using functions malloc( ) and calloc( ) is not de-allocated on their own. The free( ) function is used to
release dynamically allocated memory back to the operating system. It is essential to free memory that is no longer needed to
avoid memory leaks.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)calloc(5, sizeof(int));
// Do some operations.....
for (int i = 0; i < 5; i++)
printf("%d ", ptr[i]);
return 0;
}
Output
0 0 0 0 0
After calling free( ), it is a good practice to set the pointer to NULL to avoid using a "dangling pointer," which points to a
memory location that has been deallocated.
[Link] 4/9
11/6/25, 1:42 AM Dynamic Memory Allocation in C - GeeksforGeeks
ptr = NULL;
Syntax
free(ptr);
After freeing a memory block, the pointer becomes invalid, and it is no longer pointing to a valid memory location.
realloc( )
realloc( ) function is used to resize a previously allocated memory block. It allows you to change the size of an existing memory
allocation without needing to free the old memory and allocate a new block.
Suppose we initially allocate memory for 5 integers but later need to expand the array to hold 10 integers. We can use
realloc( ) to resize the memory block:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(5 * sizeof(int));
return 0;
}
[Link] 5/9
11/6/25, 1:42 AM Dynamic Memory Allocation in C - GeeksforGeeks
It is important to note that if realloc( ) fails and returns NULL, the original memory block is not freed, so you should not
overwrite the original pointer until you've successfully allocated a new block. To prevent memory leaks, it’s a good practice to
handle the NULL return value carefully:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(5 * sizeof(int));
// Reallocation
int *temp = (int *)realloc(ptr, 10 * sizeof(int));
return 0;
}
Practical Example
Consider the first scenario where we were having issues with the fixes size array. Let's see how we can resolve both of these
issues using dynamic memory allocation.
[Link] 6/9
11/6/25, 1:42 AM Dynamic Memory Allocation in C - GeeksforGeeks
#include <stdio.h>
#include <stdlib.h>
int main() {
return 0;
}
Output
10 20 30 40 50
In this program, we are managing the memory allocated to the pointer ptr according to our needs by changing the size using
realloc( ). It can be a fun exercise to implement an array which grows according to the elements inserted in it. This kind of arrays
are called dynamically growing arrays.
Memor y Leaks: Failing to free dynamically allocated memory leads to memory leaks, exhausting system resources.
Dangling Pointers: Using a pointer after freeing its memory can cause undefined behavior or crashes.
Fragmentat ion: Repeated allocations and deallocations can fragment memory, causing inefficient use of heap space.
Allocat ion Failures: If memory allocation fails, the program may crash unless the error is handled properly.
malloc( ) vs calloc( )
The functions malloc( ) and calloc( ) works very similar to one another. So, why there was the need for two such similar
functions.
It turns out that even though they are similar, they have different use cases due to the minor difference between them
regarding the memory initialization. malloc( ) does not initialize memory while calloc( ) initializes the memory with zero.
Article Tags : C Language C Basics C-Dynamic Memory Allocation Dynamic Memory Allocation +1 More
Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida, Gautam
Buddh Nagar, Uttar Pradesh, 201305
Company Explore
About Us POTD
Legal Job-A-Thon
Privacy Policy Connect
Careers Blogs
[Link] 8/9
11/6/25, 1:42 AM Dynamic Memory Allocation in C - GeeksforGeeks
Contact Us Nation Skill Up
Corporate Solution
Campus Training Program
Tutorials Courses
Programming Languages IBM Certification
DSA DSA and Placements
Web Technology Web Development
AI, ML & Data Science Data Science
DevOps Programming Languages
CS Core Subjects DevOps & Cloud
GATE GATE
School Subjects Trending Technologies
Software and Tools
[Link] 9/9