realloc()

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));

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn C on Codecademy