overloaded grid/matrix class for large objects
I'm trying to design a class for a 2-dimensional grid:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
template< class T> class Grid {
private:
unsigned int sizeX;
unsigned int sizeY;
T* data; // vector with data of the gridpoints
public:
Grid(unsigned int nRows, unsigned int nCols){
data = new T(nRows*nCols);
sizeY = nRows;
sizeX = nCols;
}
// acces a gridpoint:
inline T& operator()(unsigned int row, unsigned int col){
assert(row<=sizeY);
assert(col<=sizeX);
return data[row*sizeX + col];
}
};
|
This works for Grids with a small number of gridpoints i.e.
1 2 3 4
|
int main() {
Grid<double> grid(20,30);
grid(3,3)=2;
}
|
but it fails for some reasons if the dimensions are >500. (exit value 139)
Do you see a reason for this problem?
data = new T(nRows*nCols)
allocates space for one T object and assigns in the value nRows*nCols.
You wanted
data = new T[ nRows*nCols ];
Oh yes, what a simple mistake. Thank you!
Topic archived. No new replies allowed.