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?

3
  • possible duplicate of How do I declare a 2d array in C++ using new? Commented Sep 28, 2014 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. Commented Sep 28, 2014 at 17:28
  • You have a potential memory leak there. Commented Sep 28, 2014 at 17:41

1 Answer 1

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 ;)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.