Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdlib.h

C realloc() function

Reference for C realloc(): grow or shrink a heap block, keep the old data, avoid the p = realloc(p) leak, and understand the special cases of NULL and size 0.

realloc() changes the size of a block that was obtained from malloc(), calloc() or an earlier realloc(). The contents are preserved up to the smaller of the old and new sizes. If the block cannot be extended in place, realloc() allocates a new one, copies the data and frees the old block, so the address may change.

That is why the old pointer must never be used after a successful call, and why the result should never be assigned straight back to the only pointer you own: if realloc() fails it returns NULL and leaves the original block untouched, and overwriting your pointer leaks it.

C
#include <stdlib.h>

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

Try it

C A growing array, using the safe pattern

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Data survives, and shrinking to fit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
void *realloc(void *ptr, size_t new_size);
Name Description
ptr A pointer returned by malloc, calloc or realloc that has not been freed, or NULL.
new_size The new size in bytes.

Returns: a pointer to the resized block (possibly the same address, possibly a new one), or NULL on failure. On failure the original block is still valid and unchanged.

Special cases: realloc(NULL, n) is the same as malloc(n). For new_size == 0 with a non-null ptr, the behavior was implementation defined in C99 to C17 (glibc frees the block and returns NULL); C23 makes it undefined behavior, so call free() instead.

Errors / undefined behavior: passing a pointer that did not come from the allocation functions, or one already freed, is undefined. Using the old pointer after a successful move is undefined, as is reading bytes of a grown block that were never written.

Usage notes

  • Never write p = realloc(p, n); with a lone pointer. On failure you lose the only reference to the old block and leak it. Use a temporary as in the first example.
  • Grow geometrically. Doubling the capacity makes appending cost O(1) amortized. Growing by a fixed amount each time makes a loop of n appends O(n squared) in copying.
  • The new bytes are uninitialized. Growth does not zero them (unlike calloc()).
  • Pointers into the block become invalid after a call that moves it. If you keep int *last = &items[k], recompute it after a possible realloc.
  • Shrinking rarely fails and often returns the same pointer, but you still must not assume that.
  • Check the multiplication n * sizeof *p for overflow when n comes from user input.
  • Thread safety: the allocation functions are thread-safe in glibc, but resizing a block that another thread uses is not.
  • Related POSIX function: reallocarray(p, n, size) does the multiplication with an overflow check; it is not in ISO C.