0
int (**test)[4][4] = new ???[64];
for (int i = 0; i < 32; ++i)
{
    test[i] = new int[4][4][4];
}

I'm trying to create a "list" of pointers that will be initialized to NULL and then later assigned the address of a new multidimensional array of int. The for loop will (eventually) vary in number of iterations, anywhere from 0 to the full 64. I expect to end up with an array of pointers where some are valid and the rest are NULL. The problem is that I can't figure out the syntax for allocating this array of pointers. Basically, what could I put in place of those question marks?

  • possible duplicate of [How do I declare a 2d array in C++ using new?](http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new) – Cory Kramer Sep 28 '14 at 17:24
  • @Cyber - I understand how to allocate a multidimensional array on the heap, but I can't figure out the syntax for this particular case. –  Sep 28 '14 at 17:28
  • You have a potential memory leak there. –  Sep 28 '14 at 17:41

1 Answers1

1

In the name of readability, may I suggest using a typedef?

typedef int (*t)[4][4];
t* test = new t[64];

You will thank me next week when you have to maintain that horrible piece of code ;)

fredoverflow
  • 256,549
  • 94
  • 388
  • 662