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.

Syntax:

Example

calloc() function in c

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

Syntax:

Example

realloc() function in c

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

Synatx:

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.

syntax:

free (pointer_name);

Example