Dynamic memory allocation in c

Memory management in c is done using calloc( ), malloc( ), realloc( ) and free( ) functions. These functions can allocate, reallocate, deal locate and free memory during runtime. Allocation, deal location of  memory during runtime is known as dynamic memory management. It means calloc( ), malloc( ), realloc( ) and free( ) are the functions which are used for dynamic memory management.

dynamic memory allocation in c

If required memory is not available in the heap or the function fails to locate the requested memory, then the memory allocation function returns a NULL pointer.

1. malloc function:

The malloc function reserve a block of memory of specified size & returns a pointer of type void this means that we can assign it to any type of pointer. Malloc allocates a block of contiguous bytes. The allocation fails if the space in the heap is not sufficient to satisfy the request. The space allocated by malloc is not initialized. The value of allocated space will be undetermined(i.e. garbage).

Syntax of malloc:

 malloc(size);

How to use malloc: For using malloc we use the following format.

ptr_name = (cast type *)malloc(byte_size); 

during the execution of the above statement, malloc will request the heap to provide memory for storing data of cast type. If heap provides memory malloc will reserve it and will return the first byte of memory to the ptr_name.

2. calloc function:

The calloc function reserve multiple blocks of storage for n elements & returns a pointer of type void. calloc allocates multiple blocks of contiguous bytes. The allocation fails if the space in the heap is not sufficient to satisfy the request. The space allocated by malloc is initialized to zero.

Syntax of calloc:

calloc(n,element_size);

How to use calloc: For using calloc we use the following format.

ptr_name = (cast type *)calloc(n,element_size); 

during the execution of the above statement, calloc will request the heap to provide memory for storing n elements of element_size. If heap provides memory, calloc will reserve it and will return the first byte of memory to the ptr_name.

3. realloc function:

Usually, realloc() function is used to modify the size of previously allocated memory. The realloc function deallocates the old memory space and returns a pointer to a new object. The content of the new object will be the same as that of the old object prior to deallocation if the new object is greater than the old object.

Syntax of realloc:

ralloc(ptr,new_size);

How to use realloc: For using realloc we use the following format.

ptr_name = (cast type *)realloc(ptr_name,new_size);

during the execution of the above statement, calloc will deallocates the old object(memory) pointer by the ptr_name and returns a pointer to a new object that has the size specified by new_size.

4. Free function:

This function is used to free the memory that is allocated to a pointer by calloc, malloc, or realloc functions.

Syntax of free:

free( ptr_name);

How to use:

free(ptr_name);

The above statement will free the memory segment whose address is held by ptr_name.