⬅ Previous Next ➡

Dynamic Memory Management

Dynamic Memory Management in C
  • Dynamic Memory Management means allocating and freeing memory at runtime (during program execution).
  • Dynamic memory is allocated from the heap using functions from <stdlib.h>.
  • Main functions: malloc(), calloc(), realloc(), and free().
  • Always check allocation failure (NULL) and always free memory to avoid leaks.

1) malloc() - Memory Allocation

  • malloc(size) allocates a block of memory (uninitialized / garbage values).
  • Returns a pointer of type void* (typecast if needed).
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *p = (int*)malloc(5 * sizeof(int));

    if (p == NULL) {
        printf("Memory allocation failed!\n");
        return 0;
    }

    for (int i = 0; i < 5; i++) {
        p[i] = (i + 1) * 10;
    }

    for (int i = 0; i < 5; i++) {
        printf("%d ", p[i]);
    }
    printf("\n");

    free(p);
    p = NULL;

    return 0;
}

2) calloc() - Contiguous Allocation

  • calloc(n, size) allocates memory for n elements and initializes all bytes to 0.
  • Better when you need default zero values.
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *p = (int*)calloc(5, sizeof(int));

    if (p == NULL) {
        printf("Memory allocation failed!\n");
        return 0;
    }

    // Values are initially 0
    for (int i = 0; i < 5; i++) {
        printf("%d ", p[i]);
    }
    printf("\n");

    free(p);
    p = NULL;

    return 0;
}

3) realloc() - Reallocation (Resize Memory)

  • realloc(ptr, newSize) changes the size of previously allocated memory.
  • May move memory to a new location and copy old data.
  • Best practice: store result in a temporary pointer first.
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *p = (int*)malloc(3 * sizeof(int));
    if (p == NULL) return 0;

    p[0] = 10;
    p[1] = 20;
    p[2] = 30;

    // resize to 5 integers
    int *temp = (int*)realloc(p, 5 * sizeof(int));
    if (temp == NULL) {
        printf("Realloc failed!\n");
        free(p);
        return 0;
    }
    p = temp;

    p[3] = 40;
    p[4] = 50;

    for (int i = 0; i < 5; i++) {
        printf("%d ", p[i]);
    }
    printf("\n");

    free(p);
    p = NULL;

    return 0;
}

4) free() - Proper Memory Deallocation

  • free(ptr) releases heap memory back to the system.
  • After free, set pointer to NULL to avoid dangling pointer.
  • Do not use memory after free (undefined behavior).
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *p = (int*)malloc(sizeof(int));
    if (p == NULL) return 0;

    *p = 99;
    printf("Value: %d\n", *p);

    free(p);   // deallocate
    p = NULL;  // safe

    return 0;
}

5) Quick Difference (malloc vs calloc)

  • malloc: allocates memory but does not initialize (garbage values).
  • calloc: allocates memory and initializes with zero.
  • realloc: resizes previously allocated memory.

6) Quick Notes

  • Always check ptr == NULL after allocation.
  • Always call free() for allocated memory to avoid leaks.
  • Use temp pointer for realloc to avoid losing original pointer.
⬅ Previous Next ➡