Support for dynamic arrays in C++ is extremely limited. As a result most people do the following:
Use vectors from the STL (Standard Template Library)
Use specifically designed Dynamic Array classes. Below is an example posted by IanM a while back based on using vectors, although I have never used it:
#include <vector>
template <typename Content>
class Array2D
{
private:
typedef std::vector<Content> InnerVector;
typedef std::vector<InnerVector> VectorArray;
public:
typedef unsigned int IndexType;
Array2D() : Width(0), Height(0)
{ }
Array2D(int Width_, int Height_)
: Width(Width_), Height(Height_), Array( Width_, InnerVector(Height_))
{ }
typename VectorArray::reference operator[](IndexType Index)
{
return Array[Index];
}
typename VectorArray::const_reference operator[](IndexType Index) const
{
return Array[Index];
}
void SetWidth(IndexType NewWidth)
{
if (NewWidth > Width)
Array.resize( NewWidth, InnerVector(Height) );
else
Array.resize( NewWidth );
Width = NewWidth;
}
void SetHeight(IndexType NewHeight)
{
for (IndexType i = 0; i < Width; ++i)
{
Array[ i ].resize( NewHeight );
}
Height = NewHeight;
}
void SetSize(IndexType NewWidth, IndexType NewHeight)
{
SetHeight( NewHeight );
SetWidth( NewWidth );
}
IndexType GetWidth() const { return Width; }
IndexType GetHeight() const { return Height; }
private:
IndexType Width;
IndexType Height;
VectorArray Array;
};
#include <iostream>
int main()
{
Array2D<int> MyArray;
MyArray = Array2D<int>(10, 10);
int CellCount = 0;
for (Array2D<int>::IndexType y = 0; y < MyArray.GetHeight(); ++y)
{
for (Array2D<int>::IndexType x = 0; x < MyArray.GetWidth(); ++x)
{
MyArray[x][y] = CellCount;
++CellCount;
}
}
for (Array2D<int>::IndexType y = 0; y < MyArray.GetHeight(); ++y)
{
for (Array2D<int>::IndexType x = 0; x < MyArray.GetWidth(); ++x)
{
std::cout << MyArray[x][y] << ", ";
}
std::cout << 'n';
}
std::cout << 'n';
MyArray.SetWidth( MyArray.GetWidth() + 2 );
MyArray.SetHeight( MyArray.GetHeight() + 2 );
for (Array2D<int>::IndexType y = 0; y < MyArray.GetHeight(); ++y)
{
for (Array2D<int>::IndexType x = 0; x < MyArray.GetWidth(); ++x)
{
std::cout << MyArray[x][y] << ", ";
}
std::cout << 'n';
}
std::cout << 'n';
MyArray.SetSize( MyArray.GetWidth() - 2, MyArray.GetHeight() - 2 );
for (Array2D<int>::IndexType y = 0; y < MyArray.GetHeight(); ++y)
{
for (Array2D<int>::IndexType x = 0; x < MyArray.GetWidth(); ++x)
{
std::cout << MyArray[x][y] << ", ";
}
std::cout << 'n';
}
std::cout << 'n';
}
Use pointers to simulate dynamic arrays. Below is a code example I have used successfully:
Defining a 2 dimensional dynamic array in C++
// allocate a 2 dim dynamic array of n*m elements
double** pp = new double* [n];
pp[0] = new double [n*m];
for (int i = 1; i < n; ++i)
pp[i] = pp[i-1] + m;
// use pp[n][m]
// deallocation
delete [] pp[0];
delete [] pp;
Note: the individual elements like most variables in C++ are not pre initialised. If this is important to you then you will need to initialise each one first in a loop.
Hope this helps
[EDIT] Changed STD to STL
No matter how good your code is, someone will improve on it