Dynamic memory allocation in C:
The process of allocating memory during program execution is called dynamic memory allocation or run time memory allocation is known as dynamic memory allocation.
Dynamic memory allocation functions in C:
C has four functions regarding dynamic memory allocation. 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);
malloc() function in C:
malloc() function is use to allocate the run time memory in any c program. malloc() does not initialize the allocated memory initially it store 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:
malloc () function returns null pointer if it couldn’t able to allocate requested amount of memory.
Example program for malloc() function in C:
#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 memoryn"); } else { strcpy( mem_allocation,"geekcoderz.com"); } printf("Dynamically allocated memory content : " "%sn", mem_allocation ); free(mem_allocation); }
Output:
Dynamically allocated memory content :geekcoderz.com
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:
#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 memoryn"); } else { strcpy( mem_allocation,"geekcoderz.com"); } printf("Dynamically allocated memory content : " "%sn", mem_allocation ); free(mem_allocation); }
Output :
Dynamically allocated memory content : geekcoderz.com
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.
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.
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:
Example program for realloc() and free() functions in C:
#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 memoryn"); } else { strcpy( mem_allocation,"geekcoderz.com"); } printf("Dynamically allocated memory content : " "%sn", mem_allocation ); mem_allocation=realloc(mem_allocation,100*sizeof(char)); if( mem_allocation == NULL ) { printf("Couldn't able to allocate requested memoryn"); } else { strcpy( mem_allocation,"space is extended upto " "100 characters"); } printf("Resized memory : %sn", mem_allocation ); free(mem_allocation); }
Output:
Dynamically allocated memory content : geekcoderz.com
Resized memory : space is extended upto 100 characters
Resized memory : space is extended upto 100 characters