Dynamic memory allocation

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.

Syntax:

Example

Output

 

calloc() function in c

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

Syntax:

Example

Output

 

realloc() function in c

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

Synatx:

 

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.

syntax:

Example

Output

Leave a Comment