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);
Call
What 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
Parameter
Description
ptr
Existing allocation pointer, or NULL.
new_size
Requested new size in bytes.
Return Value
Return Value
Description
Pointer
Resize succeeded; the old pointer is invalid even if the address is unchanged.
NULL
Resize 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;
}
Output
40
How It Works
Step 1. Create and initialize the original two-element block.
Step 2. Request four elements through a temporary pointer.
Step 3. Replace the owner only after the resize succeeds.
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;
}
Output
9
How It Works
Step 1. Pass NULL as the old allocation.
Step 2. realloc() then behaves like malloc().
Step 3. Check and initialize the returned storage.
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;
}
Output
old value = 5
(typical result)
How It Works
Step 1. Own a valid initial allocation.
Step 2. Make an intentionally enormous resize request.
Step 3. Keep p unchanged when the temporary result is NULL.
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.