私は行列クラスを持っており、次のコンストラクターを使用しています。
template<class T>
Matrix<T>::Matrix(unsigned rows, unsigned cols) :
rows(rows), cols(cols) {
index = 0;
data_ = new T[rows * cols];
}
template<class T>
Matrix<T>::~Matrix() {
delete[] data_;
}
行列の逆行列を計算するとき、次のような一時変数のメモリを解放したいと思いますa
。
template<class T>
Matrix<T> Matrix<T>::inverse() {
unsigned i, j, k;
Matrix<T> a(2 * rows, 2 * rows);
....
return tmp;
}
この変数は関数の最後で破棄されると思いましたが、テストすると次のようになります。
for (int i = 0; i < 3; i++) {
Matrix<double> m(5, 5);
m << 5, 2, 4, 5, 6, 1, 3, 1, 2, 5, 2, 5, 2, 7, 2, 9, 2, 1, 0.1, 0.43, 1, 0, 0, 0, 1;
m.inverse();
std::cout << m << std::endl;
}
最初のループでa
は、はゼロで初期化されますが、次のステップでは、の初期値はa
前の値であるため、a(k+1)=a_endvalues(k)
。なんでこんな感じ?