Featured Article

C++ Dynamic Array of Arrays

William Aug 14, 2026

When dealing with dynamic memory allocation in C++, arrays often prove themselves to be a powerful tool. However, accessing arrays of arrays in dynamic memory can be tricky. This article delves into the intricacies of C++ dynamic arrays of arrays, guiding both novices and experienced programmers through the nuances of this topic.

What is a Multidimensional array in C?
What is a Multidimensional array in C?

Before diving into dynamic arrays of arrays, let's quickly recall that dynamic arrays are arrays whose size is determined at runtime rather than at compile time. In C++, we can achieve this using the 'new' keyword. This powerful feature allows for a more flexible and efficient use of memory. Now, let's explore how we can create, access, and manipulate arrays of these dynamic arrays.

C Arrays (With Examples)
C Arrays (With Examples)

Declaring and Initializing Dynamic Arrays of Arrays

The syntax for declaring a dynamic array of arrays in C++ is not entirely intuitive, but it's not overly complex once you get the hang of it. It resembles the syntax of declaring a one-dimensional dynamic array more than anything else. Here's an example:

What are Arrays in Java?
What are Arrays in Java?

```cpp int **arr = new int*[3]; ```

In this snippet, we're declaring an array 'arr' with capacity three. Notice the two asterisks before 'int'. This indicates that each element in 'arr' is itself a pointer - in this case, a pointer to an integer. In other words, 'arr' is a dynamic array of dynamic arrays. Keep in mind, though, that these rows are not initialized, and this can quickly lead to undefined behavior if not handled correctly.

16 Repeated Addition Arrays Worksheets 2nd Grade
16 Repeated Addition Arrays Worksheets 2nd Grade

Initializing Rows

Initializing every row with 'new int[n]' for some desired size 'n' is not a straightforward task. A common technique is to use nested 'new' operators as shown:

```cpp int **arr = new int*[3]; for (int i = 0; i < 3; i++) { arr[i] = new int[5]; } ```

C Arrays Cheat Sheet (1D, 2D Arrays, Syntax, Examples & Programs)
C Arrays Cheat Sheet (1D, 2D Arrays, Syntax, Examples & Programs)

This code initializes a 3x5 dynamic array of arrays ('arr'). It's crucial to understand that this way, you're not only allocating the space for pointers (which takes 4 or 8 bytes depending on your system), but also for the integers that those pointers will point to.

Embracing Memory Management

Remember that dynamic memory requires explicit deallocation after use. Forgetting to free up memory in C++ can lead to memory leaks, reducing your program's performance over time. The following code correctly cleans up the memory after usage:

Array Data Structure - GeeksforGeeks
Array Data Structure - GeeksforGeeks

```cpp for (int i = 0; i < 3; i++) { delete[] arr[i]; } delete[] arr; ```

This ensures that every row of the dynamic array of arrays ('arr') and the array of pointers ('arr') itself is correctly deallocated.

an array of different types of fruits on a yellow background with the words arrays
an array of different types of fruits on a yellow background with the words arrays
What is 2D Array In C#
What is 2D Array In C#
C Programming - 2D Arrays
C Programming - 2D Arrays
VBA Dynamic Array (ReDim)
VBA Dynamic Array (ReDim)
Multi-Dimensional Arrays in C (3D-2D-1D Arrays): Syntax, Examples & Real Use Cases (2026 Guide)
Multi-Dimensional Arrays in C (3D-2D-1D Arrays): Syntax, Examples & Real Use Cases (2026 Guide)
How to Print 2D Array in C
How to Print 2D Array in C
Arrays in C - GeeksforGeeks
Arrays in C - GeeksforGeeks
Arrays
Arrays
Learn Arrays in C++ in 10 minutes.
Learn Arrays in C++ in 10 minutes.
Array implementation
Array implementation
Arrays in C++ – Beginner Friendly Guide
Arrays in C++ – Beginner Friendly Guide
Multidimensional Array in Java (2D Array) - Scientech Easy
Multidimensional Array in Java (2D Array) - Scientech Easy
12 Must-Know Excel Dynamic Array Functions
12 Must-Know Excel Dynamic Array Functions
Understanding Arrays in Programming: A Simple Guide with Real-World Examples
Understanding Arrays in Programming: A Simple Guide with Real-World Examples
Array in C++ Handwritten Notes 📘 | Easy Array Notes for Beginners
Array in C++ Handwritten Notes 📘 | Easy Array Notes for Beginners
Building Arrays - Free Digital Math Slides - Math Tech Connections
Building Arrays - Free Digital Math Slides - Math Tech Connections
Data structures: array
Data structures: array
Day 1 of dsa - Arrays...
Day 1 of dsa - Arrays...
an array sheet with text and numbers on it
an array sheet with text and numbers on it

Accessing and Manipulating Arrays of Arrays

Once you've declared and initialized your dynamic array of arrays, you can access and manipulate its elements like you would with regular multi-dimensional arrays, with an extra layer of indirection to account for the pointers. Here's how:

```cpp arr[0][1] = 5; // Assigns 5 to the second element of the first row int value = arr[2][3]; // Stores the value of the fourth element of the third row in 'value' ```

Bear in mind that array indices in C++ are zero-based, so don't mix up this fact with the layer of indirection introduced by the pointers.

Advanced Topics: Functions and Dynamic Arrays of Arrays

It's possible to pass dynamic arrays of arrays to functions, and here the extra layer of indirection makes things a bit more complicated. The following code demonstrates how to do so:

```cpp void printArray(int **arr, int rows, int cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { cout << arr[i][j] << " "; } cout << endl; } } ```

The function 'printArray' takes a dynamic array of arrays ('arr'), along with the number of rows and columns as arguments. This way, the caller knows the size of the array, and there's no risk of indexing out of bounds. Notice, however, that the array is passed by value, meaning that the actual array in the caller's scope won't be modified by the function; instead, a copy of the array (a copy of the pointers) is passed to the function.

Understanding the Use Cases of Dynamic Arrays of Arrays

Dynamic arrays of arrays, like one-dimensional dynamic arrays, are useful when working with variable-sized data. They provide the flexibility of dynamic memory allocation combined with the convenience of array syntax. They're particularly suited for applications where space is a concern and you need to deal with multi-dimensional data, such as in game development or high-frequency trading systems.

In the end, working with dynamic arrays of arrays in C++ can be a daunting task at first, but once you grasp the fundamentals, you'll discover a powerful tool for managing multi-dimensional data and memory.