Here is a way to do it that allocates the memory contiguously on the heap:
typedef double MyGrid[3][4];
int main(int argc, char* argv[])
{
MyGrid& x = *(reinterpret_cast<Grid*>(new double[12]));
...
x[1][2] = 0.3333;
...
delete[] &x;
return 0;
}
Which you could easily turn into a more generic solution:
template<typename T, int x, int y>
struct Array2D
{
typedef T CArrayType[x][y];
typedef CArrayType& RefType;
static CArrayType& New()
{
return *(reinterpret_cast<CArrayType*>(new T[x * y]));
}
static void Delete(RefType x)
{
delete[] &x;
}
};
typedef Array2D<double, 3, 4> MyGrid;// define your 2d array with 3 rows / 4 columns.
int main(int argc, char* argv[])
{
MyGrid::RefType j = MyGrid::New();
...
j[1][2] = 0.3333;
...
MyGrid::Delete(j);
return 0;
}
The idea is to just generate the elements in 1D (x*y), and cast it to a 2D array. But since array types are value types, you need to deal in pointers-to-arrays. Using a reference makes it almost transparent.
Boost probably has something like this but I don't know boost well enough to say...