C Dynamic Memory Allocation

Intermediate
16 min read
Updated: Nov 2026
1 example
10 guides

Definition and Usage

Dynamic memory allocation lets a program request memory while it runs, when sizes are not known at compile time. The C library provides malloc, calloc, realloc, and free in <stdlib.h>. Memory comes from the heap, which you must release yourself. This section explains the memory layout of a program, stack vs heap, each allocation function, dynamic arrays, 2D arrays, memory leaks, and safe habits.

Every malloc needs a free.

Match each successful allocation with exactly one free. Check allocation results for NULL, and set pointers to NULL after freeing them.

Learning Path

Work through the stages in order, or jump to a topic from the index below (10 tutorials).

C Examples

A quick taste of dynamic memory. Compile with gcc file.c -std=c11 -Wall -o out.

Example 1 — Allocate, use, and free

Create an array whose size is chosen at run time.

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

int main(void) {
    int n = 4;
    int *a = malloc(n * sizeof *a);
    if (a == NULL) return 1;

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

    free(a);
    return 0;
}

How It Works

  1. malloc(n * sizeof *a) requests space for n ints.
  2. A NULL result means the request failed.
  3. free(a) returns the memory to the system.

C Dynamic Memory Allocation Topics

Every guide in this section with a one-line summary (10 tutorials).

Memory model

2 tutorials

Where code, globals, the stack, and the heap live.

TopicDescriptionTutorial
Memory layout of a C programSee where code, globals, the heap, and the stack live.Try it
Stack and heap memoryCompare automatic stack storage with manually managed heap storage.Try it

Allocation functions

4 tutorials

malloc, calloc, realloc, and free.

TopicDescriptionTutorial
malloc()Allocate a block of uninitialized memory from the heap.Try it
calloc()Allocate zero-initialized memory for an array of elements.Try it
realloc()Resize a block while keeping its existing contents.Try it
free()Release heap memory exactly once and reset the pointer.Try it

Patterns and safety

4 tutorials

Resizable arrays, 2D allocation, leaks, and safe habits.

TopicDescriptionTutorial
Dynamic arraysBuild a resizable array that doubles its capacity as it fills.Try it
Dynamic 2D arraysAllocate a matrix of rows and columns on the heap.Try it
Memory leaksFind and fix allocations that are never freed.Try it
Safe memory practicesA checklist for allocating, using, and releasing memory safely.Try it

Edge Cases & Pitfalls

Common mistakes when learning dynamic memory in C.

leak

Forgetting free

Memory that is never freed is a leak; long-running programs eventually run out.

uaf

Use after free

Reading or writing freed memory is undefined behavior.

double

Double free

Calling free twice on the same pointer corrupts the heap. Set it to NULL after freeing.

Key Takeaways

  • Check: allocation can fail.
  • Pair: one free per allocation.
  • Reset: set freed pointers to NULL.

One line: Dynamic memory allocation lets a program request memory while it runs, when sizes are not known at compile time.

Frequently Asked Questions

It is requesting memory from the heap at run time with functions such as malloc and calloc, and releasing it with free when finished.
Stack memory is automatic, fast, and limited; it is released when a function returns. Heap memory is manual, larger, and lasts until you free it.
No. In C, void * converts to any object pointer implicitly. Casting is only needed in C++.

Next: Memory Layout of a C Program

See where code, globals, the heap, and the stack live.

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.

9 people found this page helpful