You are here:

C++/dynamic allocation



How are you?
I know how to declare dynamically an array of objects of class MyClass:

MyClass **array=new MyClass *[1000];

But how could I declare a 2-dimensional array dynamically like that. The following won't work:
MyClass **array=new MyClass *[1000][1000];
What would the correct syntax be?


ANSWER: Hi, Andres.

If you want the memory to be contiguous, then you'll need to allocate a 1D array and use some math to treat it as a 2D array.

Consider, when you declare a 2D array on the stack in C++ (standard array declaration), it's no different than a 1D array except that you can index it differently.  For example, the following:

  int grid1[20];
  int grid2[5][4];

In memory, these two arrays are laid out identically.  It's a single, contiguous block of memory that holds 20 integers.  The only difference between them is in the way that they can be indexed.  You can, in fact, index both with row/column data, you just have to do a tiny bit of math for the 1D array:

  for (int row = 0; row < numRows; ++row)
      for (int col = 0; col < numCols; ++col)
         grid1[row * numCols + col] = 0;
         grid2[row][col] = 0;

In the above code, each one writes to the same offset each time.  Using this method, you can treat a 1D array as if it were a 2D array.

If contiguous memory doesn't concern you, and you're OK with only the individual rows being contiguous, then you can use multiple new operations to generate a 2D array of pointers:

  int** grid = new int*[numRows];
  for (int i = 0; i < numRows; ++i)
      grid[i] = new int[numCols];

Using the above code, you have a single contiguous block of int*'s, which you can consider your rows.  Each int* points to a single contiguous block of ints, which you can consider your columns.  The whole of the 2D array is not contiguous in memory, but the array of row pointers is contiguous, and each row is contiguous.

OK, if I have left you confused about anything or if I've raised further questions, or even if you just have an unrelated question, feel free to let me know.

[an error occurred while processing this directive]---------- FOLLOW-UP ----------


I've been testing what you sugguested and I have this question. Why does this declaration work:

MyTree **array1 = new MyTree *[1001];
for(i=0; i<1001; i++){array1[i]=new MyTree[1001];}

But why doesn't it work together with an assignment like this:

MyTree **array1 = new MyTree *[1001];
for(i=0; i<1001; i++){array1[i]=new MyTree[1001];}
array1[1][1]=new MyTree();

In other words, why doesn't this array1 work like a normal 2D table?

Everything does work just fine when I use int like here:

 int **array1 = new int *[1001];
 for(i=0; i<1001; i++){array1[i]=new int[1001];}

At the same time I know that the MyTree class assignment works because this works just fine for me:

typedef MyTree* pointer_type;
pointer_type(*array1)[101]=new pointer_type[1000][101];
array1[1][1]=new MyTree();


Hi, Andres.

Your code:

   MyTree **array1 = new MyTree *[1001];
   for(i=0; i<1001; i++){array1[i]=new MyTree[1001];}
   array1[1][1]=new MyTree();

The problem here is that you're trying to allocate something that's already been allocated.  The value in array1[1][1] is not a pointer to a MyTree, it's an instance of the MyTree class already.  The MyTree constructor has already been called.  If you want a 2D array of MyTree pointers, then you'll need to do this:

   MyTree ***array1 = new MyTree **[1001];
   for(i=0; i<1001; i++){array1[i]=new MyTree*[1001];}
   array1[1][1]=new MyTree();

Let me know if you have any more questions!


All Answers

Answers by Expert:

Ask Experts


Joseph Moore


I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.


I've been writing C++ code for 12 years, both on my own in my spare time and professionally.


Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2017 All rights reserved.

[an error occurred while processing this directive]