In this tutorial, you will learn how to dynamically allocate memory in C program using 4 standard library functions: malloc(), calloc(), free() and realloc() with examples. You will also learn the difference between malloc and calloc at the end.
Dynamic Memory Allocation
The main concept of dynamic memory allocation in c programming enables the programmer to allocate memory to the variable at runtime. In C, dynamic memory is allocated from the heap using some standard library functions.
There may be times in a program where you may need to increase the size of an array. So to increase the array size, we can manually allocate the memory during run-time and this allocating and freeing of memory refers to dynamic memory allocation in C programming.
These are mainly four library functions in the stdlib.h header file through which memory allocation is possible in C:
- malloc()
- calloc()
- realloc()
- free()
Function | syntax |
---|---|
malloc() | malloc (number *sizeof(int)); |
calloc() | calloc (number, sizeof(int)); |
realloc() | realloc (pointer_name, number * sizeof(int)); |
free() | free (pointer_name); |
1. malloc():
This function stands for memory allocation. It is a function that is used to allocate a block of memory dynamically. It takes a single argument that is the amount of memory to allocate in bytes but does not initialize the memory allocated at execution time. So it has a garbage value initially.
Syntax for malloc function:
1 | ptr=(cast-type*)malloc(byte-size); |
It is used in the program in the following way:
In the block of code shown below, memory is allocated for space of size 8 bytes to store 8 characters.
Applying it in programming:
1 2 3 4 5 6 | //use // char pointer char *p; // allocate memory p = (char *) malloc (8 * sizeof(char)); |
Example: C Program for malloc()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <stdio.h> #include <stdlib.h> int main() { int n, i, *ptr, sum = 0; printf("Enter number of elements: "); scanf("%d", &n); ptr = (int*) malloc(n* sizeof(int)); //Checking for memory element to be null if (ptr == NULL) { printf("Sorry! Not able to allocate Memory.\n"); exit(0); } printf("Enter %d Numbers that you want to sum of: ", n); for (i = 0; i < n; ++i) { scanf("%d", ptr + i); sum += *(ptr + i); } printf("Sum of the Numbers: %d", sum); //free memory location free(ptr); return 0; } |
Output:
1 2 3 | Enter number of elements: 2 Enter 2 Numbers that you want to sum of: 5 4 Sum of the Numbers: 9 |
When the program no longer needs the dynamic array, it eventually calls free to return the memory it occupies. The malloc function returns a NULL value if it fails to allocate the specified memory space
2. calloc():
The calloc function stands for contiguous allocation. calloc() function is also like malloc () function. But calloc() initializes the specified bytes to zero. It is mainly used in complex data structures such as arrays and structures.
Calloc()
function is used to allocate multiple blocks of memory while malloc()
is used for a single block of memory.
Syntax for calloc function:
1 2 3 | ptr=(cast_type*)calloc(n,size); //n is the number of memory block of same size |
Example: C Program for calloc():
calloc() needs two arguments that are the number of variables to allocate in memory, and the size is in bytes of a single variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char *calloc_allocation; //memory allocated dynamically calloc_allocation = calloc(10, sizeof(char)); if (calloc_allocation == NULL) { printf("Couldn't able to allocate requested memory\n"); } else { strcpy(calloc_allocation, "Calloc working"); } // display result printf("Dynamically allocated content: %s\n", call_allocation); // free memory location free(calloc_allocation); } |
Output:
1 | Dynamically allocated content: Calloc working |
3. realloc():
realloc stands for reallocation of memory. realloc()
function is used to change the memory size of the previously allocated memory space or we can say that it increases or decreases the size of the block of memory that is specified to use and also realloc() moves it if necessary.
Syntax for realloc function:
1 | realloc (pointer_name, number * sizeof(int)); |
Example: C Program for relloc():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <stdio.h> int main() { char *pt; pt = (char*) malloc(15); strcpy(pt, "realloc"); printf(" %s, located = %u\n", pt, pt); pt = (char*) realloc(pt, 15); //pt is reallocated with new size strcat(pt, " in C"); printf(" %s, located = %u\n", pt, pt); free(pt); return 0; } |
Output:
1 2 | realloc, located = 38420496 realloc in C, located = 38420496 |
4. free()
The free() function is called to release/deallocate memory. it releases the specified block of memory and returns it back to the system. It frees the allocated memory of malloc (), calloc (), realloc () function because these functions does not get freed on their own.
Syntax:
1 | free (pointer_name); //it does not have return type |
C Program for free() with malloc():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <stdio.h> #include <stdlib.h> int main() { int n, i, *ptr, sum = 0; printf("Enter number of elements: "); scanf("%d", &n); ptr = (int*) malloc(n* sizeof(int)); //Checking for memory element to be null if (ptr == NULL) { printf("Sorry! Not able to allocate Memory.\n"); exit(0); } printf("Enter %d Numbers that you want to sum of: ", n); for (i = 0; i < n; ++i) { scanf("%d", ptr + i); sum += *(ptr + i); } printf("Sum of the Numbers: %d", sum); //free memory location free(ptr); return 0; } |
Output:
1 2 3 | Enter number of elements: 2 Enter 2 Numbers that you want to sum of: 5 4 Sum of the Numbers: 9 |
Difference between malloc()
and calloc()
:
malloc() | calloc() |
---|---|
Malloc() function will create a single block of memory of size specified by the user. | Calloc() function can assign multiple blocks of memory for a variable. |
The number of arguments is 2. | The number of arguments is 1. |
It is not initialized, so contains garbage values. | It always initialized to zero |
Malloc is faster as compared to calloc. | Calloc is slower as compared to malloc. |
This function returns only starting address and does not make it zero. | This function returns the starting address and makes it zero. |
Time efficiency is higher. | Time efficiency is lower. |