Prev Next
Dynamic memory allocation in C:
The process of allocating memory during program execution is called dynamic memory allocation.
Dynamic memory allocation functions in C:
C language offers 4 dynamic memory allocation functions. They are,
- 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() function in C:
- malloc () function is used to allocate space in memory during the execution of the program.
- malloc () does not initialize the memory allocated during execution. It carries garbage value.
- malloc () function returns null pointer if it couldn’t able to allocate requested amount of memory.
Example program for malloc() function in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char *mem_allocation; /* memory is allocated dynamically */ mem_allocation = malloc( 20 * sizeof(char) ); if( mem_allocation== NULL ) { printf("Couldn't able to allocate requested memory\n"); } else { strcpy( mem_allocation,"fresh2refresh.com"); } printf("Dynamically allocated memory content : " \ "%s\n", mem_allocation ); free(mem_allocation); } |
Output:
Dynamically allocated memory content : fresh2refresh.com
|
2. calloc() function in C:
- calloc () function is also like malloc () function. But calloc () initializes the allocated memory to zero. But, malloc() doesn’t.
Example program for calloc() function in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char *mem_allocation; /* memory is allocated dynamically */ mem_allocation = calloc( 20, sizeof(char) ); if( mem_allocation== NULL ) { printf("Couldn't able to allocate requested memory\n"); } else { strcpy( mem_allocation,"fresh2refresh.com"); } printf("Dynamically allocated memory content : " \ "%s\n", mem_allocation ); free(mem_allocation); } |
Output:
Dynamically allocated memory content : fresh2refresh.com
|
3. realloc() function in C:
- realloc () function modifies the allocated memory size by malloc () and calloc () functions to new size.
- If enough space doesn’t exist in memory of current block to extend, new block is allocated for the full size of reallocation, then copies the existing data to new block and then frees the old block.
4. free() function in C:
- free () function frees the allocated memory by malloc (), calloc (), realloc () functions and returns the memory to the system.
Example program for realloc() and free() functions in C:
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 |
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char *mem_allocation; /* memory is allocated dynamically */ mem_allocation = malloc( 20 * sizeof(char) ); if( mem_allocation == NULL ) { printf("Couldn't able to allocate requested memory\n"); } else { strcpy( mem_allocation,"fresh2refresh.com"); } printf("Dynamically allocated memory content : " \ "%s\n", mem_allocation ); mem_allocation=realloc(mem_allocation,100*sizeof(char)); if( mem_allocation == NULL ) { printf("Couldn't able to allocate requested memory\n"); } else { strcpy( mem_allocation,"space is extended upto " \ "100 characters"); } printf("Resized memory : %s\n", mem_allocation ); free(mem_allocation); } |
Output:
Dynamically allocated memory content : fresh2refresh.com
Resized memory : space is extended upto 100 characters |
Difference between static memory allocation and dynamic memory allocation in C:
Static memory allocation | Dynamic memory allocation |
In static memory allocation, memory is allocated while writing the C program. Actually, user requested memory will be allocated at compile time. | In dynamic memory allocation, memory is allocated while executing the program. That means at run time. |
Memory size can’t be modified while execution. Example: array |
Memory size can be modified while execution. Example: Linked list |
Difference between malloc() and calloc() functions in C:
malloc() | calloc() |
It allocates only single block of requested memory | It allocates multiple blocks of requested memory |
int *ptr;ptr = malloc( 20 * sizeof(int) );For the above, 20*4 bytes of memory only allocated in one block. Total = 80 bytes |
int *ptr;Ptr = calloc( 20, 20 * sizeof(int) );For the above, 20 blocks of memory will be created and each contains 20*4 bytes of memory. Total = 1600 bytes |
malloc () doesn’t initializes the allocated memory. It contains garbage values | calloc () initializes the allocated memory to zero |
type cast must be done since this function returns void pointer int *ptr;ptr = (int*)malloc(sizeof(int)*20 ); | Same as malloc () function int *ptr;ptr = (int*)calloc( 20, 20 * sizeof(int) ); |