3

According to typeid(array).name() it is PAN_i in G++ 4.8.4. I know P means pointer and i means int, but what type is A and why is there only second dimension - N - and not the first - M.

What should I type instead of auto in auto array = new int[M][N]?

Is there a way to assign array to int**?

Another issue is M and N must be compile-time constants. If they are not, I guess my only option is the dynamic C++03 2D arrays.

Community
  • 1
  • 1
Slazer
  • 4,750
  • 7
  • 33
  • 60
  • It would help knowing the compiler, for `name` returns a string that is implementation defined, see here: http://en.cppreference.com/w/cpp/types/type_info/name – skypjack Oct 25 '15 at 07:31
  • The compiler is GCC 4.8.2. It is the same in MinGW 4.9.2. – Slazer Oct 25 '15 at 07:32
  • I don't think that you can do `new int[M][N]` to begin with (although, maybe in C++11 you can). But you can declare `typedef int[M][N] MyArray_t` and then use `new MyArray_t` (assuming that `M` and `N` are constants). In any case, in C++ you may as well use `vector>`. Otherwise, if you still insist on using a native (C-style) array, then the type in this case should be `int**`. – barak manos Oct 25 '15 at 07:37
  • @barakmanos I think it is OK. And only `N` has to be a compile time constant. – juanchopanza Oct 25 '15 at 07:39
  • I'm flagging this question as possible duplicate of http://stackoverflow.com/questions/81870/is-it-possible-to-print-a-variables-type-in-standard-c . Moreover, you are asking three different things in a question with a title the does not cover all of them. – skypjack Oct 25 '15 at 07:41
  • @juanchopanza: As far as I recall, you need to allocate the first dimension (`int** array = new int*[M]`), and then for each entry in the array, allocate the second dimension (`array[i] = new int[N]`). – barak manos Oct 25 '15 at 07:41
  • @barakmanos It is a C++11 only feature. The `typedef int[M][N] MyArray_t` throws me `error: expected unqualified-id before '[' token typedef int[2][3] MyArray_t;`. – Slazer Oct 25 '15 at 07:43
  • Sorry, should be `typedef int MyArray_t[M][N]`. BTW, the terminology "throws an error" (more accurately "throws an exception") is reserved for runtime errors (those that occur when you execute your program). The error that you're referring to is a **compilation** error. It's not just nit-picking, knowing the differences between compile-time and runtime is an important part of your general comprehension. – barak manos Oct 25 '15 at 07:44
  • @barakmanos Re. "As far as I recall", that is doing something else. – juanchopanza Oct 25 '15 at 07:46
  • @juanchopanza: "As far as I can sense" you are patronizing, so this conversation is terminated. – barak manos Oct 25 '15 at 07:47
  • @barakmanos I am not. I am saying the thing you are describing is a different thing to `new int[M][N]`. – juanchopanza Oct 25 '15 at 07:48
  • @Slazer `new int[M][N]?` is valid in C++03 too. Of course, `N` has to be a compile time constant. – juanchopanza Oct 25 '15 at 07:50
  • @juanchopanza: OK. I'm pretty sure that the `typedef` syntax is identical (in terms of compilation, hence obviously in terms of runtime). The other allocation method that I've suggested would indeed yield a different memory structure during runtime (assuming that `new int[M][N]` is "compilable" (I thought it wasn't, but if you insist then I take your word for it)). – barak manos Oct 25 '15 at 07:51
  • @skypjack It is not a duplicate. I know `typeid(t).name()`. But its does not answer the question `What should I type instead of auto in auto array = new int[M][N]?`. – Slazer Oct 25 '15 at 07:51
  • Its not a duplicate. I needed to interpret the output of `typeid(x).name()`. – Slazer Oct 25 '15 at 08:16

1 Answers1

5

What is the type of new int[M][N]?

The type is pointer to length N array of int1, or

int (*)[N]

If you think of what a new[] expression does, it initializes an array of a length given at runtime and returns a pointer to the first element of that array. So the type of

new T[M]

is T*. In the 2D case, you can think of T as being int[N]. So each element of the array is a itself array, and new returns a pointer to its first element. This may be a clearer way of expressing it:

typedef int ArrayN[N];

auto array = new ArrayN[M];

1 And in C++, the dimensions if C-style arrays have to be compile time constants, meaning N has to be a constant expression

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • You may also want to suggest the use of `typedef int MyArray_t[M][N]`, and perhaps also recommend the use of `vector>` in this case. – barak manos Oct 25 '15 at 07:43
  • I have to use `bool**` to represent the structure. – Slazer Oct 25 '15 at 07:45
  • @barakmanos I might do. But I don't prefer to stick to answering one quesiton per post, because they should only be asking one question. – juanchopanza Oct 25 '15 at 07:47
  • This is answered. I am going to ask the other questions separately. – Slazer Oct 25 '15 at 07:57
  • 1
    @barakmanos I added a `typedef` example, but only with a 1D array, because that is the type of the elements of the array created with the new expression. – juanchopanza Oct 25 '15 at 08:01
  • Why N has to be constant expression and M does not? – Slazer Oct 25 '15 at 08:29
  • @Slazer The dimension `M` is the length of the dynamically allocated array. That array contains C-style arrays. And in C++ those need a size known at compile time. – juanchopanza Oct 25 '15 at 09:05