malloc vs calloc vs realloc

Dynamic Memory Allocation refers to the method of allocating a block of memory and releasing it when the memory is not required at the time of running the program. A block of memory can be used to store values of simple or sub-scripted variables. In C Programming Language, there are four types of Dynamic Memory Allocation exist:

malloc:

malloc() function is used to allocate a single block of memory to store values of specific data types. It assigns the address of the first byte of the allotted space to a pointer.
Syntax:

sp= (type *)malloc(size);

where sp is the pointer variable, the type is a data type that is to be stored in memory, size is the number of bytes to be allotted.

calloc:

calloc() function is used to allocate multiple blocks of the same size during the program execution. It is used to store the values of an array.
Syntax:

sp= (type *) calloc (n, m);

where sp is the pointer variable, the type is a data type which is to be stored in memory, n is the number of blocks to be allotted, and m is the number of bytes in each block of memory.

realloc:

realloc() function is used to reallocate the memory space which is previously allotted. It increases or reduces the allotted space at a later stage in a program.
Syntax:

sp=realloc(sp, size);

where sp is the pointer variable, and size is the number of bytes to be newly allotted.

free:

free() function is used to release the memory space which is allotted using malloc() or calloc() or realloc() function.
Syntax:

free(sp);

where sp is the pointer which refers to the allotted space in memory.

Difference between malloc and calloc and realloc:

malloc
calloc
realloc
1. malloc stands for memory allocations.1. calloc stands for contiguous allocation1. realloc stands for re-allocation.
2. malloc() is a function which is used to allocate a block of memory dynamically.2. calloc() is a function which is used to allocate multiple blocks of memory. 2. realloc() is a function which is used to resize the memory block which is allocated by malloc or calloc before.
3. It is used to dynamically allocate a single large block of memory with the requied size3. calloc() specifies the number of blocks of memory to be allocated.3. When memory allocated previously is not sufficient, then more memory is required, the realloc method is used to re-allocate memory dynamically.

4. Syntax: mp = (cast_type*) malloc(byte_size);4. Syntax: cp= (int*) calloc(100, sizeof(int));4. Syntax: rp = realloc(rp, newSize);