C programming realloc stdlib.h


In C programming the <stdlib.h> realloc function deallocate the old storage and returns a pointer to the new storage allocated.The declaration of the function is shown below.

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

Parameters:
mem -Pointer to the previous memory and also whose memory is also to be reallocate.Simply the memory pointed to by ‘mem’ will be enlarged.

size -The new size of the memory to be reallocated.

Return type
void*-‘NULL’ is returned if allocation fails or the pointer ‘mem’ is returned:the pointer to the reallocated memory.

Some points to note:

i) Calling ‘realloc’ to allocate memory larger than the previously allocated size preserve the content in the previous memory.The content in the new extended memory is indeterminate.

ii)If the pointer=the first argument- passed points to ‘nullptr’ or is ‘NULL’ type then the ‘realloc’ function behave like the ‘malloc’ function.

iii) If the pointer passed does not match the earlier pointer,means the pointer points to memory which is not allocated by ‘calloc’ , ‘malloc’ or ‘realloc’ call then the behavior is undefined.And also if the memory pointed by the pointer has be deallocated the behavior is undefined.

iv) If the size passed is smaller than the previous memory allocated size the result is undefined.

v) If the memory cannot be allocated then the old object is not deallocated and the content is also preserve.

Link : C malloc stdlib.h

Code example

char st[]=”Malloc” ;

char *mem=(char*)malloc( strlen(st)+1 );

memcpy( mem , st , strlen(st) ); ///copies the content of st to ‘mem’

//accessing the content stored in ‘nmem’ memory
for(size_t i=0 ; i< strlen(st) ; i++)
{
printf(“%c” , *(mem+i) );
}

mem=(char*)realloc( mem , (strlen(st)*2)+1 ); ///the memory pointed by ‘mem’ size is increased to twice of ‘st’ string length plus 1

printf(“\n”);

//Checking if the content is preserve
for(size_t i=0 ; i< strlen(st) ; i++)
{
printf(“%c” , *(mem+i) );
}

free(mem);

Output,

Malloc
Malloc

As you can see after the size of the memory is enlarged the content is still preserved.





A more exhaustive code example is discussed below.In this program we will try to enlarge the size of the memory and also append new content to the content of the previous storage.

#include <stdio.h>
#include <stdlib.h>
#include <string.h> ///strlen function is include here

int main( )
{
char st[]=”new old and happy sad” ,
  st1[]=” Due date” ;

size_t sz=25 ;

char *cmem=(char*)malloc( sz ) ;

memcpy( cmem , st , strlen(st)+1 ); //copies content of ‘st’ to ‘cmem’

//outputting the content of ‘cmem’
for(size_t i=0 ; i< strlen(st) ; i++)
{
printf(“%c” , *(mem+i) );
}

sz+=10 ;

cmem=(char*)realloc( cmem , sz );

memcpy( cmem+strlen(st) , st1 , strlen(st)+1 ); //Append content of ‘st1’ to the content found in ‘cmem’

//outputting the content of ‘cmem’
for(size_t i=0 ; i< strlen(st)+strlen(st1) ; i++)
{
printf(“%c” , *(mem+i) );
}

(cmem);

getchar();
return 0;
}

Output in Visual Studio,

new old and happy sad
new old and happy sad Due date

The second output is the content of the memory when the size is enlarged and new string is appended to the previous content.


Related links

->C calloc stdlib.h