diff --git a/source/module_hamilt_lcao/module_hcontainer/base_matrix.cpp b/source/module_hamilt_lcao/module_hcontainer/base_matrix.cpp index ff02d7744a..a7531a1245 100644 --- a/source/module_hamilt_lcao/module_hcontainer/base_matrix.cpp +++ b/source/module_hamilt_lcao/module_hcontainer/base_matrix.cpp @@ -109,8 +109,11 @@ assert(this->value_begin != nullptr); template void BaseMatrix::add_array(T* array) { - // if allocated, save data from array into matrix - // if whole matrix and 2d-block format, save data from array into matrix either +#ifdef __DEBUG +assert(this->value_begin != nullptr); +#endif + // if allocated, add data from array into matrix + // if whole matrix and 2d-block format, add data from array into matrix either for (int i = 0; i < nrow_local * ncol_local; ++i) { value_begin[i] += array[i]; @@ -120,6 +123,9 @@ void BaseMatrix::add_array(T* array) template void BaseMatrix::add_element(int mu, int nu, const T& value) { +#ifdef __DEBUG +assert(this->value_begin != nullptr); +#endif int index = mu * this->ncol_local + nu; value_begin[index] += value; } @@ -127,6 +133,9 @@ void BaseMatrix::add_element(int mu, int nu, const T& value) template T& BaseMatrix::get_value(const size_t& i_row, const size_t& j_col) const { +#ifdef __DEBUG +assert(this->value_begin != nullptr); +#endif int index = i_row * this->ncol_local + j_col; return value_begin[index]; } @@ -145,6 +154,12 @@ BaseMatrix& BaseMatrix::operator=(const BaseMatrix& other) { this->nrow_local = other.nrow_local; this->ncol_local = other.ncol_local; + + if (this->allocated) + { + delete[] this->value_begin; + } + if (other.allocated) { this->value_begin = new T[nrow_local * ncol_local]; @@ -172,6 +187,12 @@ BaseMatrix& BaseMatrix::operator=(BaseMatrix&& other) noexcept { this->nrow_local = other.nrow_local; this->ncol_local = other.ncol_local; + + if (this->allocated) + { + delete[] this->value_begin; + } + this->value_begin = other.value_begin; this->allocated = other.allocated; if (other.allocated) diff --git a/source/module_hamilt_lcao/module_hcontainer/base_matrix.h b/source/module_hamilt_lcao/module_hcontainer/base_matrix.h index d866c6f67f..ef591546a1 100644 --- a/source/module_hamilt_lcao/module_hcontainer/base_matrix.h +++ b/source/module_hamilt_lcao/module_hcontainer/base_matrix.h @@ -39,11 +39,12 @@ class BaseMatrix void set_zero(); /** - * @brief save an array to the matrix + * @brief add an array to the matrix * - * @param array array to be saved + * @param array array to be added */ void add_array(T* array); + /** * @brief add a single element to the matrix * @@ -52,6 +53,7 @@ class BaseMatrix * @param value value to be added */ void add_element(int mu, int nu, const T& value); + // for inside matrix /** * @brief get value from a whole matrix @@ -61,6 +63,7 @@ class BaseMatrix * @return T& */ T& get_value(const size_t& i_row, const size_t& j_col) const; + /** * @brief get pointer of value from a submatrix */