realloc() function in C

This “realloc() function in C” is providing clear information about how to increase or decrease the size of array using realloc() function once we allocate the memory using calloc() function in Dynamic memory allocation.


 

realloc :

  • Used to re-acquire the memory in DMA.
  • Used to increase or decrease the size of Array in DMA.
  • Prototype of realloc() function defined as follows in stdlib.h header file.

void* realloc(void* ptr, size_t size);

  • Using malloc function, we allocate memory for structure variables.
  • Mostly data structure algorithms implementation like linked lists, trees and graphs uses struture data type to create nodes.
  • First argument specifies the address of memory block from which it has to re-aquire the space
  • Second argument specifies new size of block.
  • On success, returns first byte address.
  • On failure, returns NULL pointer.

 

​for example in a linked list, node structure will be…
struct node
{
int data ;
struct node* left;
};

realloc() function in C

 

  • But in case of arrays, once if we allocate the memory using calloc() function, that will be the fixed memory to store and process the elements.
  • We can’t increase and decrease the size of the memory block.
  • realloc( ) functions is used to increase or decrease the size of memory block.
  • realloc( ) is mostly used to modify the size of array which has created dynamically.

 

Share this