# Thread: dynamic allocation of multidimensional array

1. ## dynamic allocation of multidimensional array

I am trying to allocate memory for a 2 dimensional array of size declared by the user but am not quite sure how to do this. I read several forums about it to generate the code I have so far but am getting errors about have invalid types int[int] for array subscript. Any help would be appreciated THANKS.

Code:
```#include <iostream>
#include <vector>
using namespace std;

void MagicSquare ( int ptr, int n );
void DisplaySquare ( int ptr, int n );

int main(void)
{
int order, **ptr;

cout <<"This program will create a magic square of an odd order\n";
cout <<"Please enter the order number you would like:\n";
cin >> order;
ptr = new int *[order]; //allocates a new array of pointers to int type
for(int i=0; i<order; ++i)
ptr[i]=new int[order]; //allocates space for each pointer making second dimension
if (order%2==1)
MagicSquare( **ptr, order );
else{
delete []ptr;
cout <<"order cannot be even\n";
}
DisplaySquare( **ptr, order );
}
void MagicSquare ( int ptr, int n )
{
int max = n * n;
int i=0;
int j=n/2;     // start position

for (int k=1; k<=max; ++k)
{
ptr[i][j]= k;

i--;
j++;

if (k%n == 0)
{
i += 2;
--j;
}
else
{
if (j==n)
j -= n;
else if (i<0)
i += n;
}
}
}
void DisplaySquare( int ptr, int n)
{
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
cout << ptr[i][j] << "\t";

cout << "\n\n\n\n";
}

}```

2. You're creating the 2D array properly, but you're calling and declaring your subroutines wrong:
Code:
```MagicSquare(ptr, order);
...
void MagicSquare(int **ptr, int n)

// same for display func```
ptr is a bad name. square is a better one.

And your code code be formatted better.

3. Originally Posted by lancehumiston
I am trying to allocate memory for a 2 dimensional array of size declared by the user but am not quite sure how to do this.
There are a few ways to do this, but quite possibly the easiest (though slightly space inefficient) is to #include <vector> and use a std::vector of vectors:
Code:
`vector<vector<int> > magic_square(order, vector<int>(order));`
Now you just access magic_square[i][j]. Since this is a standard container, you do not need to do manual memory management. Of course, your functions have to change accordingly, e.g.,
Code:
`void DisplaySquare(const std::vector<std::vector<int> >& magic_square);`
A typedef can help.

4. It's also possible to use a single dimensional array or vector and mimic a 2D array or vector like this:
Code:
`v[y * order + x]`
That way you can allocate it as a 1D array or order*order size.
Easier to allocate. More space efficient. Faster to access (I think). Easier to delete.
The only downside is you can't access it like v[x][y].

5. Originally Posted by oogabooga
The only downside is you can't access it like v[x][y].
Of course, a function can be written to approximate such access, especially since it can be unit tested to avoid mistakes such as writing y * order + x instead of x * order + y, given a row-major order

6. Or you could use Boost.MultiArray, which wraps all this stuff up in a nice class.

Popular pages Recent additions