C Dynamic 2D Arrays

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Dynamic 2D Arrays?

There are two common ways to allocate a 2D array. (1) An array of row pointers: allocate rows pointers, then allocate each row. This gives m[r][c] syntax. (2) A single contiguous block of rows * cols elements accessed as m[r * cols + c], which needs only one free and is cache-friendly.

Remember
/* row pointers */
int **m = malloc(rows * sizeof *m);
for (r...) m[r] = malloc(cols * sizeof **m);

/* one block */
int *a = malloc(rows * cols * sizeof *a);
a[r * cols + c] = value;

Cheat Sheet

Keep these facts handy while you work with Dynamic 2D Arrays in C.

TopicDetail
Row pointersrows + 1 allocations, m[r][c]
One block1 allocation, index math
Free orderRows first, then the pointer array
ContiguityOnly the single block is contiguous

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Memory Leaks.

Example 1 — A 3 x 4 grid with row pointers

Fill, print, and free every row.

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

int main(void) {
    int rows = 3, cols = 4;
    int **m = malloc(rows * sizeof *m);
    if (!m) return 1;

    for (int r = 0; r < rows; r++) {
        m[r] = malloc(cols * sizeof **m);
        if (!m[r]) return 1;
        for (int c = 0; c < cols; c++) m[r][c] = r * cols + c;
    }

    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) printf("%2d ", m[r][c]);
        printf("\n");
    }

    for (int r = 0; r < rows; r++) free(m[r]);
    free(m);
    return 0;
}

How It Works

  1. m is an array of rows pointers, each pointing to a row of cols ints.
  2. Value at (r, c) is r * cols + c.
  3. Free every row first, then the pointer array.

Edge Cases & Pitfalls

Check these before relying on Dynamic 2D Arrays in real programs.

leak

Freeing only m

Calling free(m) alone leaks every row. Free rows first.

fail

Partial failure

If a row allocation fails midway, free the rows you already allocated before returning.

Key Takeaways

  • Two levels: free in reverse.
  • Single block: simpler and faster.
  • Check every allocation.

One line: There are two common ways to allocate a 2D array.

Frequently Asked Questions

Either allocate an array of row pointers and then each row, or allocate one block of rows * cols elements and index it as r * cols + c.
For row pointers, free each row and then the array of pointers. For a single block, one free is enough.
Continue with "C Memory Leaks" next. Find and fix allocations that are never freed. The C Dynamic Memory Allocation hub lists every topic in order.

Next: Memory Leaks

Find and fix allocations that are never freed.

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.

10 people found this page helpful