Dynamic memory allocation

tutorial and example
tutorial and example

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file.

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

The process of allocating memory during program execution is called dynamic memory allocation. It returns NULL if the memory is not sufficient.  It does not initialize the memory allocation during the execution.



calloc() function in c

The calloc() function is used to allocate the multiple block of request memory.   It allocates the memory with zero-initialize.



realloc() function in c

The realloc() function is used to modify the allocated memory size by malloc and calloc functions to new size.


realloc (pointer_name, number * sizeof(int));

free() function in c

The free() function is used to free the allocated memory by malloc(), calloc() functions and return the memory of the system.


free (pointer_name);