# Thread: 2D arrays with variable indexes

1. ## 2D arrays with variable indexes

I'm writing a program (a random map generator, to be exact) that requires a 2D array to created base on two variables (for the map), height and width. These variables are to be decided at runtime, but the strange way of creating a pointer to a 2D array on the heap has me stumped since the lesser index has to be known. How would I do this with both indexes to be decided at runtime?

2. You will have to either use something along the lines of nested std::vectors (recommended), or dynamically allocate the memory. Here is a small example:
Code:
```int x = ...;
int y = ...;

int** array = new int*[x];

for(int i = 0; i != x; ++i)
array[i] = new int[y];

// And now, clean up...

for(int j = 0; j != x; ++j)
delete[] array[i];
delete[] array;```
If you are not too familiar with pointers, this probably isn't a good route. The vectors are really a better (easier and safer) idea.

3. I'm familiar with pointers, but the line of my knowledge is drawn before all of that double indirection and 2D array stuff.

After I declare the array in the way you posted, I can use the array as normal (array[i][j])?

4. Yes... You'd use it normally.

5. I used this, and kept getting one of those Microsoft error things (the ones where you have the option to send an error report) whenever I ran the program. Any idea what's up?

6. one of those Microsoft error things
The most common cause of this is trying to access memory that doesn't belong to you. For example, reading 1 past the end of an array.

I'd also point out that 2d arrays aren't pretty. You'll need to write functions to create and destroy the array, and the syntax for passing 2d arrays to functions is clunky.

There are two ways to deal with this. If you just need something that works, the vector of vectors is by far the simplest solution. If you want to get a feel for 2d arrays, writing a class (matrix or grid or some such) is a good exercise, as it really begs for encapsulation.

7. AH_Tze's points are really good, and I recommend paying attention to them.

However, to find the error specifically, we will need code.

8. While I was writing this post, I thought of the problem. I was trying to access the array in a function I never passed it to (at least I think that's what the problem is). The code is 2500 lines long anyway, so I don't think anyone wants to look at it. If that's not the problem, I'll post the code for you guys to take a look at.

-thanks

9. I got it to work, but I want to be able to understand it. Tell me if I'm wrong in any of these.

Code:
`int** array = new int*[x];`
This declares a pointer to a pointer and then initializes it to point to the first element of an array of pointers (the array having x elements).

Code:
```for(int i = 0; i != x; ++i)
array[i] = new int[y];```
This assigns the pointer that array[i] points to to an array with i elements.

Code:
`(*(*(array+i)+j)) = 5;`
This dereferences array (along with adding an offset before the dereference), to access the pointer pointed to by (array+i). Each element of array holds a pointer that holds the address of an int array on the heap so further applying the dereference operator on and adding an offset to array (array[i][j]) dereferences the pointer pointed to by array[i] and accesses the element array[i][j].

I have a hunch this is right. I didn't do any research, this is just an educated guess. I'm fairly good with pointers already, just not pointers to pointers.

10. Originally Posted by Zach L.
Code:
```int x = ...;
int y = ...;

int** array = new int*[x];```
What's wrong with:
Code:
`int array[x][y];`
?

11. You can't have a 2D array with both indexes unknown.

Edit: I guess you can. Nevermind.

12. Originally Posted by Kleid-0
What's wrong with:
Code:
`int array[x][y];`
?
Array sizes have to be constant expressions. Because x and y aren't const, the declaration is ill-formed. If your compiler supports it then it does so as a nonstandard extension.

13. Is it standard for 1D arrays to not have constant sizes?

14. >>Is it standard for 1D arrays to not have constant sizes?
No, that's a consistent rule. This is legal:
Code:
```int main()
{
int a[5];
int b[10][10];
}```
And this is legal:
Code:
```int main()
{
const int size1 = 5;
const int size2 = 10;
int a[size1];
int b[size2][size2];
}```
But this is not:
Code:
```int main()
{
int size1 = 5;
int size2 = 10;
int a[size1];
int b[size2][size2];
}```