×
☰ Menu

Dynamic Memory Allocation

As you know, before using an array, its size must be declared. In order to hold data, the array you declared might be small or too large. This problem can be resolved via dynamic memory allocation.

Managing memory manually is referred to as dynamic memory management. This enables you to acquire extra memory as needed and release it when not required.

 

Dynamic memory allocation is the allocation of memory during the runtime or during the program execution. Dynamic memory allocation provides different functions in the C programming language. They are: malloc(), calloc(), realloc(), free().

 

there are 4 library functions defined under <stdlib.h> for dynamic memory allocation.

Function

Use of Function

malloc()

Allocates the requested size of bytes and returns a pointer first byte of allocated space

calloc()

Allocates space for array elements, initializes to zero, and then returns a pointer to memory

free()

deallocate the previously allocated space

realloc()

Change the size of previously allocated space

 

malloc()

The name malloc stands for "memory allocation".

This method allocates the space in the memory during execution but will not initialize the memory allocation during execution as it carries garbage values and if it cannot allocate requested memory then it returns a null pointer.

Syntax of malloc()

ptr = (cast-type*) malloc(size-in-byte);

ptr is a pointer of cast-type. The malloc() function returns a pointer to an area of memory with size of byte size.

ptr = (int*) malloc(50 * sizeof(int));

This statement will allocate either 100 or 200 according to the size of int 2 or 4 bytes respectively and the pointer points to the address of first byte of memory allocated. 

 


calloc()

The name calloc stands for "contiguous allocation".

This is also known as contiguous allocation. As in malloc() will not initialize any memory bit. But in calloc() it allocates the memory along with initializing the bits to zero.

The main difference between malloc() and calloc() is that malloc() allocates single piece of memory whereas calloc() allocates sets of blocks of memory each of the same size.

Syntax of calloc()

ptr = (cast-type*)calloc(n, element-size);

This statement will allocate contiguous space in memory for an array of n elements. For example:

ptr = (float*) calloc(50, sizeof(float));


This statement allocates contiguous space in memory for an array of 50 elements each of the size of the float.

 


free()

As discussed above the memory space should be freed or released if not in use. In Dynamic memory allocation, malloc() and calloc() function only allocates memory but cannot free the memory on their own so this is done using free() method explicitly to release the memory that is not in use to avoid memory leakage

realloc()

As the name suggests, in dynamic memory allocation if suppose a user wants to allocate more memory which means more memory than specified or required by the program then we can use this realloc() function to alter the size of memory that was allocated previously.