Dynamic Memory Allocation in C | C Dynamic Memory Allocation using malloc(), calloc(), free() and realloc()

The process of allocating and deallocating (freeing) memory at the time of execution of a program (at runtime) is called dynamic memory allocation.


The advantage of this method is that only as much memory as is needed at any time is reserved, thus avoiding unnecessary blocking of memory as in static memory allocation. Two types of problem may occur in static memory allocation. If number of values to be stored is less than the size of memory, there would be wastage of memory. If we would want to store more values by increase in size during the execution on assigned size then it fails.

Allocation and release of memory space can be done with the help of some library functions.


Memory Allocation and Deallocation Functions:


Functions

Purpose

malloc()

Allocates the memory of requested size.

calloc()

Allocates multiple block of requested memory.

free()

Frees the previously allocated memory.

realloc()

Reallocates the memory (modifies the size of previously allocated memory)



Malloc() Function:


The malloc or memory allocation function is used to allocate a block of memory of the specified size (in bytes). It returns a pointer to the first byte of allocated space. The type of the pointer it returns is void, which can be cast into a pointer of any type. It returns NULL, if there is not enough memory available. The malloc() function does not initialize memory at execution time, so it has garbage value initially.

Syntax: 

ptr=(cast_type*)malloc(size_in_bytes)


Example:  

 int *x;

  x=(int*)malloc(sizeof(int));


The above statement allocates 2 bytes of main memory and returns the address of the memory allocated which is stored in x. The (int *) before the malloc function casts the integer type on malloc, hence malloc returns a pointer to the integer.


Calloc() Function:


The calloc or contiguous allocation function works similar to malloc() function except for the fact that it requires two parameters or arguments as against one parameter needed by malloc(). It allocates multiple block of requested memory.


Syntax:      

ptr=(cast_type*)calloc(n, size_in_bytes);


Here, n is the number of blocks and size_in_bytes specifies size of each block

calloc() initializes each block with a default value 0. It returns NULL, if there is not enough memory available.

Example:

float *y;

y=(float *) calloc(5, sizeof(float));

This statement allocates contiguous space in memory for 5 elements of type float.


Realloc() Function:


The realloc or re-allocation function is used to resize the size of memory block, which is already allocated. In short, it changes the memory size.

This function is used to expand or shrink the allocated memory block. It is useful in two applications:

 1. If the allocated memory block is insufficient for current application.

      2.  If the allocated memory is much more than what is required by the current application


Syntax:      

ptr=realloc(ptr, new_size);

Here, ptr is reallocated with a new size,  new_size


Free() Function:


Just as memory can be dynamically allocated during runtime, it can also be de-allocated when not needed.

When an element is no longer required, the memory allocated to it must be freed so that it is available for future use. This is done by the function free(). It helps to reduce wastage of memory by freeing it.

Syntax:      

free(ptr);

This statement frees the space allocated in the memory pointed by ptr.


Post a Comment

0 Comments