Dynamic Memory Allocation in C

    Atul Kabra3 min readUpdated

    Dynamic memory allocation in C lets your program request memory while it is running, instead of fixing the amount when you write the code. You ask for a block with malloc, use it through a pointer, and give it back with free. This is how you build data structures whose size you don't know in advance.

    Why you need it

    A fixed array needs a size at compile time: int a[100];. But what if the user decides how many items they have? Dynamic allocation lets you ask for exactly the amount you need at runtime. This memory comes from the heap and lives until you free it.

    You'll need to include <stdlib.h>.

    malloc and free

    malloc(bytes) reserves a block of uninitialized memory and returns a pointer to it (or NULL if it fails). Use sizeof so the size is correct on any platform:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
        int n = 5;
    
        /* Ask for room for n ints */
        int *arr = malloc(n * sizeof(int));
        if (arr == NULL) {            /* ALWAYS check for failure */
            printf("Allocation failed.\n");
            return 1;
        }
    
        for (int i = 0; i < n; i++) {
            arr[i] = i * 10;          /* use it just like an array */
        }
        printf("First: %d, Last: %d\n", arr[0], arr[n - 1]);
    
        free(arr);    /* give the memory back to the system */
        arr = NULL;   /* avoid using a dangling pointer afterwards */
        return 0;
    }
    

    Every malloc must be paired with exactly one free. Forgetting to free causes a memory leak.

    Want to learn this properly?

    Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.

    Explore C Programming

    calloc: allocate and zero

    calloc(count, size) allocates memory for count items of size bytes each and sets every byte to zero:

    int *zeros = calloc(5, sizeof(int));   /* 5 ints, all initialized to 0 */
    if (zeros != NULL) {
        /* use zeros... */
        free(zeros);
    }
    

    The difference from malloc: calloc zeroes the memory, malloc leaves it as garbage.

    realloc: resize a block

    realloc grows or shrinks a previously allocated block, preserving the existing contents up to the smaller size:

    int *arr = malloc(3 * sizeof(int));
    /* ... later we need more room ... */
    int *bigger = realloc(arr, 6 * sizeof(int));
    if (bigger != NULL) {
        arr = bigger;   /* only overwrite arr after confirming success */
    }
    

    Assign to a temporary first: if realloc fails it returns NULL but leaves the original block intact, so you don't lose your only pointer to it.

    The golden rule

    For every successful allocation, there must be exactly one matching free, and you must not use the memory after freeing it. Track who "owns" each block.

    Common mistakes

    • Not checking for NULL after malloc/calloc/realloc — allocation can fail.
    • Memory leak: losing the only pointer to allocated memory, or never calling free.
    • Use-after-free: using a pointer after free — undefined behaviour. Set the pointer to NULL after freeing.
    • Double free: calling free twice on the same block — a crash. Setting to NULL after the first free helps (free(NULL) is safe).
    • Overwriting your pointer in realloc before checking it succeeded — you leak the original block on failure.

    FAQ

    malloc or calloc? Use calloc when you want the memory pre-zeroed; malloc when you'll fill it yourself.

    What's a dangling pointer? A pointer that still holds an address after that memory has been freed. Dereferencing it is undefined; null it out.

    Continue at the hub C Programming, then Pointers in C and Arrays in C.

    Want to learn this properly? Join the waitlist for our C Programming course — taught in Jalgaon.

    Want to learn this properly?

    Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.

    Explore C Programming
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides