Ugh! Raw arrays & pointers
If you want auto-expanding arrays, use an STL vector - you can even get jagged arrays with vectors.
#include <vector>
std::vector< std::vector<float> > MyArray( 100, std::vector<float>(100) );
... although I wouldn't use that directly in code myself - still too error prone. I'd wrap it in it's own class:
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];
}
IndexType GetWidth() const { return Width; }
IndexType GetHeight() const { return Height; }
private:
IndexType Width;
IndexType Height;
VectorArray Array;
};
(Don't worry - already had this lying around
).
You'd use it like this:
Array2d<float> MyArray(10,5); // Define an array of 10x5
MyArray[1][1] = 1.2345;
float v = MyArray[3][4];
You can even copy one array into another with this class, and you'll notice - not one pointer, memory allocation or raw array in sight to muck up on.