Allocating multi-dimensional arrays in C++

July 23rd, 2003 at 1:35 pm

Allocating multi-dimensional arrays in C++ (and C) is not a trivial task for newbies, and many find it difficult. The concept itself isn’t really that hard to understand, but the cryptic syntax of C++ can sometimes make it far from obvious.

The important point to understand here is that in C++, a multi-dimensional array is actually an array of arrays. Thinking in these terms, the task becomes quite easy. It is best to illustrate this with an example:


#include 
#include 

using namespace std;

int main(int argc, char *argv[])
{
    unsigned i;

    // Declaration of the two-dimensional array
    // as a pointer to pointer
    //
    char** array_2D;
    const unsigned ROWS = 10;
    const unsigned COLUMNS = 10;

    // Allocate "main" array
    //
    array_2D = new char*[ROWS];

    // Allocate each member of the "main" array
    //
    for (i = 0; i < ROWS; ++i)
        array_2D[i] = new char[COLUMNS];

    // Fill the 6th element with a string and
    // print it out
    //
    strcpy(array_2D[5], "Hey there");
    cout << array_2D[5] << endl;

    // Deletion is performed in reversed order.
    // Pay special attention to the delete[]
    // operator which must be used to delete
    // arrays (instead of the "simple" delete)
    //
    for (i = 0; i < ROWS; ++i)
        delete[] array_2D[i];

    delete[] array_2D;

    return 0;
}

In this example, an 2-dimensional array of char is created. As you probably know, an array of char is usually used in the role of a text string in C & C++, so a 2-dimensional array is then an array of strings.

Dynamic allocation and reclamation of multi-dimensional arrays is not an entirely trivial task. It is also quite “dangerous”, as memory leaks and/or buffer overflows may be easily produced. If all you need is an array of character strings, it is highly adviced to use a vector of strings from the C++ standard library.

Related posts:

  1. Initialization of structures and arrays in C++
  2. Correct usage of const with pointers
  3. Reading C type declarations
  4. c/c++ annoyance - unsigned iteration
  5. Initializing an array in constant time

6 Responses to “Allocating multi-dimensional arrays in C++”

  1. Evert MouwNo Gravatar Says:

    Nice article about multidimensional arrays! I was looking for something like this. Your example is very clear and avoids confusing by using a simple delete [] array[i] instead of pointer notation.

  2. lenNo Gravatar Says:

    very nice and smart :) was looking for smth like that for all day :D

  3. Bill FrostNo Gravatar Says:

    Thanks Eli, this worked first time.

  4. MooNo Gravatar Says:

    People incapable of this shouldnt code anyways.

  5. supersaurusNo Gravatar Says:

    or do it in one shot instead:

    // the parens are necessary
    char (*p2d)[COLUMNS] = new char[ROWS][COLUMNS];

    p2d[1][1] = ‘x’;

    delete [] p2d; // don’t forget the []

    you can easily see that constructors and destructors are properly called by allocating an array of a simple class that writes output from them.

  6. MikaNo Gravatar Says:

    Ah!, Finally thank you very much!.
    Was looking all over for this.

    I actually tried it before I found this, just didn’t think of the need for the reverse deallocation.

    It is kinda weird that, my beginners book(Which i am done with!), GameDev, and some other C++ sites, which include beginner guides did not include this(Or they just made it really hard to find)

    Well thank you again, now i can continue.

Leave a Reply

To post code with preserved formatting, enclose it in `backticks` (even multiple lines)