in Technology by
What is the purpose of realloc( )?

1 Answer

0 votes
by

The realloc function is used to resize the allocated block of the memory. It takes two arguments first one is a pointer to previously allocated memory and the second one is the newly requested size. The realloc function first deallocates the old object and allocates again with the newly specified size. If the new size is lesser than the old size, the contents of the newly allocated memory will be the same as prior but if any bytes in the newly created object goes beyond the old size, the values of the object will be indeterminate.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char *pcBuffer = NULL;
/* Initial memory allocation */
pcBuffer = malloc(8);
strcpy(pcBuffer, "aticle");
printf("pcBuffer = %s\n", pcBuffer);
/* Reallocating memory */
pcBuffer = realloc(pcBuffer, 15);
strcat(pcBuffer, "world");
printf("pcBuffer = %s\n", pcBuffer);
free(pcBuffer);
return 0;
}

Output:

pcBuffer = aticle
pcBuffer = aticleworld

Note: It should only be used for dynamically allocated memory but if ptr is the null pointer, realloc behaves like the malloc function.

Related questions

0 votes
    The Three Horizons of Purpose led Growth are (1)Build digital core (2)Drive growth & transformation (3)Transform and Grow around Purpose-led Ecosystem (4)Innovate business models...
asked Sep 21, 2021 in Technology by JackTerrance
0 votes
    What is the purpose of the dfsadmin tool?...
asked Aug 5, 2021 in Technology by JackTerrance
0 votes
    What’s the purpose of the Selenium Grid? 1. To run multiple tests in parallel across multiple browsers and ... To allow cross-browser testing 4. To perform load testing...
asked Jul 11, 2021 in Technology by JackTerrance
0 votes
    What's the purpose of automation testing? 1. It allows developers to build faster applications. 2. It reduces the bugs ... It makes it easier for testers to test the applications....
asked Jul 11, 2021 in Technology by JackTerrance
0 votes
    What is the purpose of the volume parameter in a docker run command?...
asked Jun 20, 2021 in Technology by JackTerrance
0 votes
    What is a volatile variable and there purpose n Java?...
asked May 19, 2021 in Technology by JackTerrance
0 votes
    The basic purpose of the toLocaleString() is to _________ (1)return a localised object representation (2)return a ... (4)return a localized string representation of the object...
asked May 18, 2021 in Technology by JackTerrance
0 votes
    Explain the purpose of tPigLoad component....
asked Mar 23, 2021 in Technology by JackTerrance
0 votes
    What is the purpose of ‘tXMLMap’ component?...
asked Mar 23, 2021 in Technology by JackTerrance
0 votes
    Explain the purpose of tDenormalizeSortedRow. in Talend?...
asked Mar 18, 2021 in Technology by JackTerrance
0 votes
    What is the purpose of WSDL in a web service?...
asked Mar 13, 2021 in Technology by JackTerrance
0 votes
    What is the purpose of SOAP in a web service?...
asked Mar 13, 2021 in Technology by JackTerrance
0 votes
    What is the purpose of XML in a web service?...
asked Mar 13, 2021 in Technology by JackTerrance
0 votes
    Hackers usually used the computer virus for ______ purpose. To log, monitor each and every user's stroke To gain access ... 's data stored in the computer system All of the above...
asked Mar 6, 2021 in Technology by JackTerrance
0 votes
    What is the purpose of the keyword research?...
asked Mar 5, 2021 in Technology by Editorial Staff
...