C++: Dynamically Allocated Multi-Dimensional Arrays

Dynamically Allocated Multi-Dimensional Arrays

In this C++ video tutorial, we demonstrate how to allocate multi-dimensional arrays dynamically. When we do not know what size our array should be until runtime, we need to allocate the memory dynamically at runtime. The basis for allocating multi-dimensional arrays is allocating a one-dimensional arrays. We can allocate and deallocate a one-dimensional array of three ints like this:

To allocate a 2D array, we use a double pointer, or a pointer to a pointer, as the base. In the video, we showed how to create a 3x2 2D array of ints. For that array, we allocated an array of 3 int pointers for the first dimension and 3 arrays of 2 ints for the second dimension, as shown below.

The structure of the dynamically-allocated 2D array above used one array of int pointers and three arrays of ints. To demonstrate the flexibility of dynamic allocation, we will create the same 2D array using just one int array allocation for the entries instead of the three that we used for the program in the video. The code for our program looks like this.

#include <iostream>

int main()
{
    using namespace std;

    // Allocate a two-dimensional 3x2 array of ints
    int** ippArray = new int*[3];
	ippArray[0] = new int[6];
	ippArray[1] = &(ippArray[0][2]);
	ippArray[2] = &(ippArray[0][4]);

    // Fill the array
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 2; ++j) {
            ippArray[i][j] = i + j;
        }
    }

    // Output the array
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 2; ++j) {
            cout << ippArray[i][j] << "  ";
        }
        cout << endl;
    }
    cout << endl;

    // Deallocate
	delete [] ippArray[0];
    delete [] ippArray;

    // Keep the window open
    cin.get();
    return 0;
}

Note the differences between this program and the one in the video. First, we allocate an array of six ints and then we set the second and third row pointers to point to the third and fifth entries of the allocated array. Also, for our deallocation, we only deallocate one row and then we deallocate the row pointer array. Compiling and executing the program, we get the same output that we had for the program in the video.

The program above does exactly the same thing as the program in the video. However, the structure of the the memory allocation is completely different, as we can see below. Instead of three int arrays of 2 ints, we have just one array of 6 ints. The entries initial of array int pointers is set to point to the first, third, and fifth entries of the int array. Much like the our string program in video that showed the size of our rows could vary, this program demonstrates that we can vary the layout of memory for our dynamic arrays. This importance of this flexibility will become more clear as we get deeper into data structures, algorithms, and optimization. For now, we mainly want to emphasize the fact that dynamic allocations allow us to accomodate whatever amount of memory is required at runtime.