Dynamic Memory Allocation
Dynamic memory allocation reserves memory at runtime on the heap using malloc, calloc, realloc, and releases it with free.
Introduction
Dynamic memory allocation reserves memory at runtime on the heap using malloc, calloc, realloc, and releases it with free. In C17/C23, guard against integer overflow in size math, use sizeof *ptr, and verify every allocation under AddressSanitizer or Valgrind.
Understanding the topic
malloc & calloc malloc allocates uninitialized bytes; calloc(n, sizeof *p) zero-initializes and guards against n * size integer overflow. Always check for NULL.
free & sanitizer checks Release memory once, set pointer to NULL (C23: nullptr), and test with -fsanitize=address,undefined to catch heap-buffer-overflow and use-after-free.
- malloc & calloc — malloc allocates uninitialized bytes; calloc(n, sizeof *p) zero-initializes and guards against n * size integer overflow.
- Safe realloc pattern — Assign realloc to a temporary pointer so a NULL failure does not leak the original block.
- free & sanitizer checks — Release memory once, set pointer to NULL (C23: nullptr), and test with -fsanitize=address,undefined to catch heap-buffer-overflow and use-after-free.
Step-by-step explanation
- malloc & calloc — malloc allocates uninitialized bytes; calloc(n, sizeof *p) zero-initializes and guards against n * size integer overflow.
- Safe realloc pattern — Assign realloc to a temporary pointer so a NULL failure does not leak the original block.
- free & sanitizer checks — Release memory once, set pointer to NULL (C23: nullptr), and test with -fsanitize=address,undefined to catch heap-buffer-overflow and use-after-free.
Syntax reference
Syntax reference:
int *ptr = calloc(n, sizeof *ptr);free(ptr);ptr = NULL;
Informative example
Example program:
#include <stdio.h>#include <stdlib.h>int main(void) {size_t n = 5;int *arr = calloc(n, sizeof *arr); /* zeroed & overflow-checked */if (!arr) return 1;for (size_t i = 0; i < n; i++) arr[i] = (int)(i * 2);printf("%d\n", arr[4]);free(arr);arr = NULL; /* or nullptr in C23 */return 0;}
Output
8
Execution workflow
malloc & calloc
malloc allocates uninitialized bytes; calloc(n, sizeof *p) zero-initializes and guards against n * size integer overflow.
Code examples
Safe realloc pattern
Assign realloc to a temporary pointer so a NULL failure does not leak the original block.
int *tmp = realloc(arr, new_n * sizeof *arr);if (!tmp) { free(arr); return 1; }arr = tmp;
Best practices
- Enable warnings: gcc -Wall -Wextra -std=c11 source.c -o app
- Give every variable a defined value before it is read.
- Stay inside array bounds — C will not stop you from over-running a buffer.
Common mistakes
- Reading uninitialized storage — behavior is undefined.
- Dismissing compiler warnings instead of fixing root causes.
- Ignoring NULL returns from malloc, fopen, and similar APIs.
Hands-on exercise
Practice problems:
- Build a growable dynamic array using calloc + safe temporary-pointer realloc
- Compile with gcc -std=c17 -g -fsanitize=address,undefined and verify 0 errors
- Intentionally write arr[n] = 99 and read the AddressSanitizer heap-buffer-overflow report
Summary
Dynamic Memory Allocation in C — Heap APIs: malloc, calloc, realloc, free.