C realloc() Function

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is realloc() Function?

void *realloc(void *ptr, size_t new_size) changes the size of a block. It may extend it in place or move it, copying the old contents. On failure it returns NULL and leaves the old block untouched, so never assign the result directly to your only pointer.

Remember
int *tmp = realloc(a, new_n * sizeof *a);
if (tmp == NULL) { /* a is still valid; handle error */ }
else a = tmp;

Cheat Sheet

Keep these facts handy while you work with realloc() Function in C.

TopicDetail
SuccessReturns the (possibly moved) block
FailureReturns NULL, old block intact
New bytesUninitialized
realloc(NULL, n)Same as malloc(n)

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: free() Function.

Example 1 — Grow an array from three to six elements

Use a temporary pointer so a failure cannot leak the original block.

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

int main(void) {
    int *a = malloc(3 * sizeof *a);
    if (!a) return 1;
    for (int i = 0; i < 3; i++) a[i] = i + 1;

    int *tmp = realloc(a, 6 * sizeof *a);
    if (!tmp) { free(a); return 1; }
    a = tmp;
    for (int i = 3; i < 6; i++) a[i] = i + 1;

    for (int i = 0; i < 6; i++) printf("%d ", a[i]);
    printf("\n");

    free(a);
    return 0;
}

How It Works

  1. The first three values are preserved by realloc.
  2. The temporary pointer protects a if the call fails.
  3. The new slots are uninitialized, so the loop fills them.

Edge Cases & Pitfalls

Check these before relying on realloc() Function in real programs.

leak

a = realloc(a, n)

If it fails, a becomes NULL and the original block leaks. Use a temporary.

stale

Stale pointers

After realloc moves the block, any other pointers into the old block dangle.

Key Takeaways

  • Temp: assign to a temporary first.
  • Contents: preserved up to the smaller size.
  • Moves: old addresses can become invalid.

One line: void *realloc(void *ptr, size_t new_size) changes the size of a block.

Frequently Asked Questions

It returns NULL and leaves the original block unchanged and still allocated.
Yes. It keeps the first new_size bytes and may release the rest.
Continue with "C free() Function" next. Release heap memory exactly once and reset the pointer. The C Dynamic Memory Allocation hub lists every topic in order.

Next: free() Function

Release heap memory exactly once and reset the pointer.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

12 people found this page helpful