2

I am trying to Create a 2-D integer Array on Heap in C++. I mistakenly did this:

int** a = new int*[5][6];

The IDE doesn't show any error but while compiling I get the below error:

error: cannot convert 'int* (*)[6]' to 'int**' in initialization

I have found efficient ways to create a 2-D array but I am just curious what exactly is happening above and what does the error mean?

Venky
  • 101
  • 1
  • 10
  • 3
    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) – STF Jun 26 '16 at 04:52
  • I have read that. But couldn't find the kind of initialization I did above. – Venky Jun 26 '16 at 04:53
  • @Venky What's happening is you have the problem in that question, plus an extra (needless and incorrect) `*` causing the type to be wrong. – Iskar Jarak Jun 26 '16 at 04:55
  • Can you please elaborate a bit?. I read that other answer and couldn't find this kind of initialization anywhere and why is the IDE not showing any error but when I compile I get it?. I am using Jetbrains Clion. – Venky Jun 26 '16 at 04:58
  • @Venky the IDE is irrelevant, just ignore that (it generally won't catch type problems). The compiler tries to compile this and discovers you are trying to assign a pointer to an array of integer pointers (`int* (*)[6]`) to a pointer to a pointer to an integer (`int **`) – Iskar Jarak Jun 26 '16 at 05:02
  • As the linked answer says, you'll need to allocate in 2 stages, which you're not doing: first allocate for the pointers-to-pointers (call them rows) `new int*[number_of_rows]` and then fill that array ("row") of pointers with pointers to `int`s in the other dimension ("columns"). The first allocation allocates `int*`s, while the 2nd allocates the actual `int`s. – mkal Jun 26 '16 at 05:05
  • Ya I got that from the linked answer. My question is what exactly is happening in the above allocation and why do I get that error when I compile. – Venky Jun 26 '16 at 05:07
  • @mkal I interpret this question to be about interpreting the error message, not about how to allocate the array correctly. IMO the title is poor. – Iskar Jarak Jun 26 '16 at 05:07
  • OK; my mistake. But wouldn't `new int*[5]` allocate a pointer to an array of [5] integer pointers? And `new int*[5][6]` would allocate 6 pointers to arrays of 5 int pointers? Better question than I thought :) – mkal Jun 26 '16 at 05:34
  • @mkal Yes. even I'm wondering the same. Please upvote if you find it good. I need to increase my rep badly :) – Venky Jun 26 '16 at 05:38

1 Answers1

0

Fixed-size arrays in C++ are spelled with std::array:

std::array<std::array<int,5>,6> arr;

Variable-size arrays are spelled with std::vector:

std::vector<std::vector<int>> arr;

You should almost never use new with either of these (or anywhere else for that matter). But if you really have to, no problem:

auto arr = new std::array<std::array<int,5>,6>;

This should be very rarely needed indeed. Prefer wrapping your array in a problem-oriented class, and managing heap-allocated objects of said class with smart pointers.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • thanks for this. I wanted to know what's going on with the code in the question and interpret the error. – Venky Jun 26 '16 at 18:43