C
Craig Nicol
Hi,
Although I've been using C++ for a while, I've only recently started
writing my own template classes so forgive me if this is a silly question.
I have a matrix class, mgMatrix, that is templatised so that it can be a
matrix of any type. It has three data members, two integers for the
number of rows and columns in it, and a vector of size rows*columns to
hold all the data.
I am trying to test the speed of this class on a 10000*10000 matrix and
I am running into memory problems. Specifically when using my own +=
operator defined as:
template<class S> mgMatrix<T> operator+=(S scalar) {
std::transform(begin(), end(), begin(), std::bind2nd(std:lus<T>(),
scalar)); return *this; };
During testing, T=int and S=int (I use two types here to allow int
scalars to be added to double matrices), and scalar=0.
The memory error comes because this function calls the mgMatrix copy
constructor and the call to vector.resize(10000*10000) returns an
out-of-memory exception.
BTW, mgMatrix::begin() and mgMatrix::end() are wrappers for the vector
functions of the same name.
Can anyone tell me why the copy constructor is called from the above
function, when invoked as: testMatrix += 0;?
If required, I can post the full source code online.
Cheers,
Craig Nicol.
Although I've been using C++ for a while, I've only recently started
writing my own template classes so forgive me if this is a silly question.
I have a matrix class, mgMatrix, that is templatised so that it can be a
matrix of any type. It has three data members, two integers for the
number of rows and columns in it, and a vector of size rows*columns to
hold all the data.
I am trying to test the speed of this class on a 10000*10000 matrix and
I am running into memory problems. Specifically when using my own +=
operator defined as:
template<class S> mgMatrix<T> operator+=(S scalar) {
std::transform(begin(), end(), begin(), std::bind2nd(std:lus<T>(),
scalar)); return *this; };
During testing, T=int and S=int (I use two types here to allow int
scalars to be added to double matrices), and scalar=0.
The memory error comes because this function calls the mgMatrix copy
constructor and the call to vector.resize(10000*10000) returns an
out-of-memory exception.
BTW, mgMatrix::begin() and mgMatrix::end() are wrappers for the vector
functions of the same name.
Can anyone tell me why the copy constructor is called from the above
function, when invoked as: testMatrix += 0;?
If required, I can post the full source code online.
Cheers,
Craig Nicol.