Allocating multi-dimensional arrays in C++
July 23rd, 2003 at 1:35 pmAllocating 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:

November 24th, 2006 at 1:27 pm
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.
March 18th, 2007 at 12:32 am
very nice and smart
was looking for smth like that for all day 
April 27th, 2007 at 4:42 am
Thanks Eli, this worked first time.
August 27th, 2007 at 3:32 pm
People incapable of this shouldnt code anyways.
August 3rd, 2008 at 5:25 pm
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.
August 7th, 2008 at 4:48 pm
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.