C realloc() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<stdlib.h>

Definition

The C realloc() function changes the size of an allocation. Existing bytes are preserved up to the smaller old and new sizes. The block may move, and newly added bytes are uninitialized.

This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. Success returns the resized block and invalidates the old pointer value. For a nonzero request, failure returns NULL while the original block remains allocated. The examples below move from the direct call to an edge condition and a practical use.

Syntax

Start with this concrete realloc() call:

C
realloc(buffer, new_count * sizeof *buffer);
CallWhat it does
realloc(buffer, new_count * sizeof *buffer);Changes the size of an allocation using the concrete values shown.

Include <stdlib.h>. Also include headers that declare any types, limits, locale tools, or I/O functions used around the call.

Library declaration
C
void *realloc(void *ptr, size_t new_size);

Parameters

ParameterDescription
ptrExisting allocation pointer, or NULL.
new_sizeRequested new size in bytes.

Return Value

Return ValueDescription
PointerResize succeeded; the old pointer is invalid even if the address is unchanged.
NULLResize failed for a nonzero size; the original allocation remains valid.

C Examples

Example 1 — Grow an array safely

This complete program demonstrates a distinct part of the realloc() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *p = malloc(2 * sizeof *p);
    if (!p) return EXIT_FAILURE;
    p[0]=10; p[1]=20;
    int *tmp = realloc(p, 4 * sizeof *p);
    if (!tmp) { free(p); return EXIT_FAILURE; }
    p=tmp; p[2]=30; p[3]=40;
    printf("%d\n", p[3]);
    free(p); return 0;
}

How It Works

  1. Step 1. Create and initialize the original two-element block.
  2. Step 2. Request four elements through a temporary pointer.
  3. Step 3. Replace the owner only after the resize succeeds.
  4. Step 4. Use the expanded space and release the final block.

Example 2 — Use realloc as malloc

This complete program demonstrates a distinct part of the realloc() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *p = realloc(NULL, sizeof *p);
    if (!p) return EXIT_FAILURE;
    *p = 9; printf("%d\n", *p);
    free(p); return 0;
}

How It Works

  1. Step 1. Pass NULL as the old allocation.
  2. Step 2. realloc() then behaves like malloc().
  3. Step 3. Check and initialize the returned storage.
  4. Step 4. Free the successful allocation.

Example 3 — Preserve the old block on failure

This complete program demonstrates a distinct part of the realloc() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

int main(void) {
    int *p = malloc(sizeof *p);
    if (!p) return EXIT_FAILURE;
    *p=5;
    int *tmp = realloc(p, SIZE_MAX);
    if (!tmp) printf("old value = %d\n", *p);
    else p=tmp;
    free(p); return 0;
}

How It Works

  1. Step 1. Own a valid initial allocation.
  2. Step 2. Make an intentionally enormous resize request.
  3. Step 3. Keep p unchanged when the temporary result is NULL.
  4. Step 4. The original value remains usable and can still be freed.

Frequently Asked Questions

realloc() changes the size of an allocation. Existing bytes are preserved up to the smaller old and new sizes. The block may move, and newly added bytes are uninitialized.
Include <stdlib.h> before calling realloc().
Success returns the resized block and invalidates the old pointer value. For a nonzero request, failure returns NULL while the original block remains allocated.
Assign the result to a temporary pointer. Direct assignment can leak the original allocation when realloc() returns NULL.
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.

Next: srand()

Continue through the C stdlib.h tutorials.

srand() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful