Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
C Programming

realloc() in C

Resize previously allocated blocks with realloc.

realloc() in C — a practical guide to C realloc with clear examples you can reuse in real projects.

C Programming Tutorial Series (66/102). Prefer one article? Read the complete C programming tutorial.

Short description

Use a temporary pointer — realloc can fail and return NULL without freeing old memory.

realloc safe pattern

int *tmp = realloc(a, new_count * sizeof *a);
if (!tmp) {
    /* a still valid */
    return -1;
}
a = tmp;

Leave a reply

Your email address will not be published. Required fields are marked *