ufechner
25th September 2011, 20:10
Hello,
I am trying to build a class, that can hold a resizable two
dimensional array of double values.
I need this, to hold a matrix of 1e6 rows and 5 or more colums of real-time data.
When the data aquisition is running I want to avoid to allocate memory, because that can be slow.
But I want to be able to change the exact number of rows and columns in a configuration file (and
in a menu option, when the aquisition is not running), and therefore I need a dynamic array.
The following code crashes:
class Q2DDoubleVector : public QVector< QVector<double> > // Overloading Class QVector to allow for 2 dimensional vectors.
{
public:
// constructor
Q2DDoubleVector() : QVector< QVector<double> >(){}
// methods
// matrix resize
void mresize(int rows, int columns);
// destructor
virtual ~Q2DDoubleVector() {}
};
void Q2DDoubleVector::mresize(int rows, int columns)
{
this->resize(rows);
for(int r=0; r<rows; r++) {
this[r].resize(columns);
}
}
Q2DDoubleVector matrix;
matrix.mresize(ROWS, COLS);
If I do it in a very similar way, it doesn't crash:
QVector<QVector <double> > matrix;
matrix.resize(ROWS);
for(int r=0; r < ROWS; r++) {
matrix[r].resize(COLS);
}
Why does the first approach not work?
I don't want to implement the resize loop for each 2D matrix, that I use again.
You can download both projects (the example that works (ArrayTest) and
the example that doesn't work (ArrayTest2) from the following URL:
http://db.tt/IUWpBCnY
Any hints welcome.
Actually, I was using Delphi in the past, and with Delphi this was very easy:
var Matrix: array of array of Double;
begin
SetLength(Matrix, 10, 20)
end;
Best regards:
Uwe Fechner
I am using Ubuntu 11.04 32 bit and QT 4.7.2 .
I am trying to build a class, that can hold a resizable two
dimensional array of double values.
I need this, to hold a matrix of 1e6 rows and 5 or more colums of real-time data.
When the data aquisition is running I want to avoid to allocate memory, because that can be slow.
But I want to be able to change the exact number of rows and columns in a configuration file (and
in a menu option, when the aquisition is not running), and therefore I need a dynamic array.
The following code crashes:
class Q2DDoubleVector : public QVector< QVector<double> > // Overloading Class QVector to allow for 2 dimensional vectors.
{
public:
// constructor
Q2DDoubleVector() : QVector< QVector<double> >(){}
// methods
// matrix resize
void mresize(int rows, int columns);
// destructor
virtual ~Q2DDoubleVector() {}
};
void Q2DDoubleVector::mresize(int rows, int columns)
{
this->resize(rows);
for(int r=0; r<rows; r++) {
this[r].resize(columns);
}
}
Q2DDoubleVector matrix;
matrix.mresize(ROWS, COLS);
If I do it in a very similar way, it doesn't crash:
QVector<QVector <double> > matrix;
matrix.resize(ROWS);
for(int r=0; r < ROWS; r++) {
matrix[r].resize(COLS);
}
Why does the first approach not work?
I don't want to implement the resize loop for each 2D matrix, that I use again.
You can download both projects (the example that works (ArrayTest) and
the example that doesn't work (ArrayTest2) from the following URL:
http://db.tt/IUWpBCnY
Any hints welcome.
Actually, I was using Delphi in the past, and with Delphi this was very easy:
var Matrix: array of array of Double;
begin
SetLength(Matrix, 10, 20)
end;
Best regards:
Uwe Fechner
I am using Ubuntu 11.04 32 bit and QT 4.7.2 .