realloc()

Published Aug 18, 2022Updated Aug 19, 2022
Contribute to Docs

The realloc() function is used to reallocate a block of memory. If the memory that was previously allocated using the malloc() or calloc() function is not sufficient, it can be reallocated using the realloc() function.

Syntax

realloc(memoryblock, size);

The memoryblock must exist beforehand. The size is of type size_t.

Example

The following example showcases the realloc() function:

int *ptr;
// Memory allocated using malloc() function
ptr = (int*) malloc(5 * sizeof(int));
// ptr is reallocated with 40 bytes (4 * 10) of memory
ptr = realloc(ptr, 10 * sizeof(int));

All contributors

Looking to contribute?

Learn C on Codecademy