# Creating a multideminsional array with new

• 03-26-2007
warfang
Creating a multideminsional array with new
This works:

int * temp = new int [10];

but this won't work:

int * temp = new int[10][2];

I don't understand why. When I try to do the latter I get this error:

"Cannot convert 'int[*][2]' to 'int*' in initialization.

I would also like to be able to enter in values to the array like this:

temp[0][1] = 1;

I'm sure if that's the right syntax yet because I can't get the array to be initialized properly. What am I doing wrong?
• 03-26-2007
You might need to use a double pointer. You need to do something like this:

int **p;
p = new int*[n];
for(int i = 0; i < n; i++)
*(p+i) = new int[m];

Someone might be able to tell you another way though.

Then you can use p[x][y] = z;
• 03-26-2007
Do you know any of the dimensions of the array when you define it? For example, if you didn't know the first dimension, but you knew that the second dimension was 2 you could define it as:
Code:

```int (*array)[2]; array = new int[10][2];```
If you don't know either dimension I think (don't quote me on this) that you can get away with:
Code:

```int **array = new int*[10]; for (int i = 0; i < 10; i++) {         array[i] = new int[2]; }```
• 03-26-2007
King Mir
Quote:

You might need to use a double pointer. You need to do something like this:

...

Someone might be able to tell you another way though.

Then you can use p[x][y] = z;

Code:

```int **p; p = new int*[n]; for(int i = 0; i < n; i++)     p[i] = new int[m];```
But you should avoid dynamic arrays in C++.
• 03-26-2007
warfang
I just tried what you suggested.
Code:

```int **temp; temp = new int*[10][2];  //if I omit the '*' it still gives the same error```
I'm getting this error now:
"cannot convert 'int*[*][2]' to 'int**' in assignment"

If I make temp a pointer in the second line (ie *temp = ...) then I get the same error as I stated in my first post.
• 03-26-2007
warfang
Quote:

Do you know any of the dimensions of the array when you define it? For example, if you didn't know the first dimension, but you knew that the second dimension was 2 you could define it as:
Code:

```int (*array)[2]; array = new int[10][2];```
If you don't know either dimension I think (don't quote me on this) that you can get away with:
Code:

```int **array = new int*[10]; for (int i = 0; i < 10; i++) {         array[i] = new int[2]; }```

The first dimension is variable and can be any positive integer. The second dimension will always be 2.
• 03-26-2007
Quote:

Originally Posted by King Mir
Code:

```int **p; p = new int*[n]; for(int i = 0; i < n; i++)     p[i] = new int[m];```

They both work. p[i] = *(p+i)
• 03-26-2007
King Mir
Quote:

They both work. p[i] = *(p+i)

That's not all I fixed. You had p as a char **.

Subscripting is better, because it is easier to understand in a glance.
• 03-26-2007
warfang
Thanks so much for the help guys. I just have one more question.

I understand why this works:

Code:

```int **p; p = new int*[n]; for(int i = 0; i < n; i++)     p[i] = new int[m];```
Well kind of, I'm still a little confused on the double pointer but I understand how the 2 dimensional array comes out of it.

What I don't understand though is how this works:

Code:

```int (*array)[2]; array = new int[10][2];```
Could you explain that part to me please?
• 03-26-2007
Daved
Are you trying to use this in a program or just trying to learn about it? If you are using it in a program a vector of vectors is probably a much better solution.

If you are trying to learn about it, you should also make sure you know how to delete the arrays as well.
• 03-27-2007
robatino
If you know in advance the number of rows, and how long each of the rows is, and you want all the allocated memory to be contiguous to reduce memory fragmentation and cache misses, you can allocate a single 1D array of the necessary length. If all of the rows are the same length, then do something like this (assuming m columns and n rows):
Code:

`  int *p = new int[m*n];`
Then you can access the (i, j) element with the notation p[m*i + j]. To clean up,
Code:

`  delete[] p;`
If the n rows have different lengths, then do
Code:

```  int **p = new int*[n];   p[0] = new int[num_elements];```
where num_elements is the total number of elements in the array (equal to the sum of the lengths of all the rows). If the rows have length len0, len1, len2, ... then set
Code:

```  p[1] = p[0] + len0;   p[2] = p[1] + len1;   // etc.```
Then you can access the (i, j) element with the notation p[i][j]. To clean up,
Code:

```  delete[] p[0];   delete[] p;```
Edit: If you don't know in advance either the number of rows, or the lengths of each of the rows, then you have to use vectors instead, to be able to do the dynamic resizing (in which case the memory isn't contiguous anymore).