Pointers/Memory Allocation   «Prev  Next»
Lesson 12 Dynamic multidimensional arrays
Objective Explore program that implements dynamic 2-dimensional array.

C++ Dynamic Multidimensional Arrays

C++ does not allow general dynamic multidimensional arrays. Scientists, engineers, and others make heavy use of general two-dimensional arrays called matrices. It would be inconvenient to have to always write special routines for each possible row size declared.
Over the next four lessons, we will look at how a dynamically allocated two-dimensional array can be implemented. This code will be very helpful in writing the course project program.
  • The two struct First we will declare a struct that creates the new type twod.
    // Dynamic arrays in two dimensions.
    struct twod {
       double**  base;
       int   row_size, column_size;
    };
    

Dynamically allocate a 2D array in C++

To dynamically allocate a 2D array in C++, you need to allocate memory for an array of pointers first, and then for each pointer, allocate memory for the array of elements. Here is a step-by-step guide:
  1. Declare a pointer to a pointer:
    int** array;
    
  2. Allocate memory for an array of pointers:
    array = new int*[numRows];
    

    Here, `numRows` is the number of rows in the 2D array.
  3. Allocate memory for each row:
    for (int i = 0; i < numRows; ++i) {
      array[i] = new int[numCols];
    }
    

    Here, `numCols` is the number of columns in each row.
  4. Accessing elements in the 2D array:
    You can access elements in the 2D array using the standard array indexing syntax:
    array[row][col] = value;
    

    Here, `row` is the row index and `col` is the column index.
  5. Deallocating the memory: It’s important to free the allocated memory to avoid memory leaks. First, you need to delete the memory allocated for each row, and then delete the memory allocated for the array of pointers.
    for (int i = 0; i < numRows; ++i) {
       delete[] array[i];
    }
    delete[] array;
    

Here is a complete example:
#include <iostream>

int main() {
    int numRows = 3;
    int numCols = 4;

    // Step 1: Declare a pointer to a pointer
    int** array;

    // Step 2: Allocate memory for an array of pointers
    array = new int*[numRows];

    // Step 3: Allocate memory for each row
    for (int i = 0; i < numRows; ++i) {
        array[i] = new int[numCols];
    }

    // Using the 2D array
    for (int i = 0; i < numRows; ++i) {
        for (int j = 0; j < numCols; ++j) {
            array[i][j] = i * numCols + j;
        }
    }

    // Display the 2D array
    for (int i = 0; i < numRows; ++i) {
        for (int j = 0; j v numCols; ++j) {
            std::cout << array[i][j] << " ";
        }
        std::cout << std::endl;
    }

    // Step 5: Deallocate the memory
    for (int i = 0; i < numRows; ++i) {
        delete[] array[i];
    }
    delete[] array;

    return 0;
}

In this example, we dynamically allocate a 2D array with 3 rows and 4 columns, initialize it with values, print those values, and then properly deallocate the memory.


struct Declaration

The struct declaration creates the new type twod. The two-dimensional array has its base address stored in the member base. The row and column sizes also will be stored. The underlying data structure is very simple:
  1. The pointer base is a pointer to a pointer to double.
  2. The base pointer contains the starting address of an array of pointers, and each pointer is a starting address of a row of doubles.

Next, we will look at the allocate() and deallocate() functions.

SEMrush Software