You should be getting an error on the line with the new
int[][4]
. There must be an expression inside the []
.
Otherwise, how can the compiler know how much to allocate.
(Some quick checks with VC++, which erroneously does accept this
expression, shows that it treats it as the equivalent of new
int[0][4]
, and allocates 0 bytes. It doesn't compile with
g++.)
And of course, you confuse things additionally by abusing
auto
. The one reason never to use auto
is that you don't
know the actual type, and auto
means you don't have to know it
(until you want to use the variable, of course). In this case,
you should definitely not use auto
, because the type is a bit
special, and you want your readers to know it:
int (*aa)[4] = new int[8][4];
Once you see this, it should be obvious that aa[0]
has type
int[4]
, and there's no way you can assign a pointer to it,
since it isn't a pointer.
Of course, in C++, you'd never write anything like this anyway.
You'd define a Matrix2D
class, and use it. (And it would
probably use a std::vector<int>
to get the memory, and
calculate the single index from the two you give. Internally,
of course, so you don't have to think about it.) There is, in
fact, never a case for using array new
.