# Thread: Creating a multideminsional array with new

1. ## Creating a multideminsional array with new

This works:

int * temp = new int ;

but this won't work:

int * temp = new int;

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

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

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

temp = 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? 2. 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; 3. 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);
array = new int;```
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*;
for (int i = 0; i < 10; i++)
{
array[i] = new int;
}``` 4. Originally Posted by Link_26 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++. 5. I just tried what you suggested.
Code:
```int **temp;
temp = new int*;  //if I omit the '*' it still gives the same error```
I'm getting this error now:
"cannot convert 'int*[*]' 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. 6. Originally Posted by Brad0407 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);
array = new int;```
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*;
for (int i = 0; i < 10; i++)
{
array[i] = new int;
}```
The first dimension is variable and can be any positive integer. The second dimension will always be 2. 7. 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) 8. Originally Posted by Link_26 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. 9. 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);
array = new int;```
Could you explain that part to me please? 10. 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. 11. 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 = 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 = p + len0;
p = p + len1;
// etc.```
Then you can access the (i, j) element with the notation p[i][j]. To clean up,
Code:
```  delete[] p;
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). Popular pages Recent additions 