Relloc

relloc() function is used to change the size of a dynamically allocated block of memory.
If memory is not sufficient for malloc() or calloc(), you can reallocate the memory by realloc() function.
Syntax :

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

void ptr : is the pointer to the starting address of the existing block
size : size of the new block

Tips :
int *a = (int*)realloc(a,0); //this is equivalent to free() as this will deallocate the complete block of a.
int *a=(int *)realloc(NULL,n*sizeof(int)); //this is equivalent to malloc(). This only creates new block of memory of size n.

Note :
  • 1. If the size of the new block is larger then the size of the previous block then the machine will create entirely new block and copy the previous data written in the previous block of memory to the new block of memory.

  • 2. If the contiguous block of memory is available with the existing block, then the existing block will be extended.

Post a Comment

0 Comments