繁体   English   中英

永恒赋值运算符调用循环 C++ 模板 class

[英]Eternal assignment operator call loop C++ template class

我有一个矩阵 class ,其中包括其成员函数的子集,

#ifndef _CMATRIX_CUH_
#define _CMATRIX_CUH_

#include <thrust/device_vector.h>
#include <assert.h>
template<class T>
class CMatrix
{
private:

    size_t n_rows, n_cols;
    
    T **data;

    __host__ __device__ void allocate_data();

    __host__ __device__ void deallocate_data();
    
public:

    __host__ __device__ CMatrix(const size_t n_rows);

    __host__ __device__ CMatrix(const size_t n_rows, const size_t n_cols);

    __host__ __device__ CMatrix(const CMatrix &other);

    __host__ __device__ ~CMatrix();

    __host__ __device__ CMatrix& operator=(const CMatrix &rhs);
}

/****************************************************************************************
*  Name     : CMatrix
*  Function : Class constructor, initializes parameters and variables
*  Method   : 
*  Author   : 
*  Modified :
*****************************************************************************************/
template <class T>
__host__ __device__ CMatrix<T>::CMatrix(
    const size_t n_rows                                         // In: Amount of matrix rows
    ) :
    n_rows(n_rows), n_cols(n_rows)
{
    allocate_data();
}

template <class T>
__host__ __device__ CMatrix<T>::CMatrix(
    const size_t n_rows,                                        // In: Amount of matrix rows
    const size_t n_cols                                         // In: New amount of matrix columns
    ) :
    n_rows(n_rows), n_cols(n_cols)
{
    allocate_data();
}

template <class T>
__host__ __device__ CMatrix<T>::CMatrix(
    const CMatrix<T> &other                                     // In: Matrix/vector to copy
    ) :
    n_rows(other.n_rows), n_cols(other.n_cols)
{
    allocate_data();
    for (size_t i = 0; i < n_rows; i++)
    {
        for (size_t j = 0; j < n_cols; j++)
        {
            data[i][j] = other.data[i][j];
        }
    }
}

/****************************************************************************************
*  Name     : ~CMatrix
*  Function : Class destructor
*  Method   : 
*  Author   : 
*  Modified :
*****************************************************************************************/
template <class T>
__host__ __device__ CMatrix<T>::~CMatrix()
{
    deallocate_data();
}

/****************************************************************************************
*  Name     : operator=
*  Function : 
*  Author   : 
*  Modified :
*****************************************************************************************/
template <class T>
__host__ __device__ CMatrix<T>& CMatrix<T>::operator=(
    const CMatrix<T> &rhs                                       // In: Right hand side matrix/vector to assign
    )
{
    if (this == &rhs)
    {
        return *this;
    }
    deallocate_data();

    return *this = CMatrix<T>(rhs);
}

/****************************************************************************************
    Private functions
****************************************************************************************/
/****************************************************************************************
*  Name     : allocate_data
*  Function : 
*  Author   : 
*  Modified :
*****************************************************************************************/
template <class T>
__host__ __device__ void CMatrix<T>::allocate_data()
{
    assert(n_rows > 0 && n_cols > 0);
    data = new T*[n_rows];
    for (size_t i = 0; i < n_rows; i++)
    {
        data[i] = new T[n_cols];
    }
}

/****************************************************************************************
*  Name     : deallocate_data
*  Function : 
*  Author   : 
*  Modified :
*****************************************************************************************/
template <class T>
__host__ __device__ void CMatrix<T>::deallocate_data()
{
    if (data == nullptr)
    {
        return;
    }

    for (size_t i = 0; i < n_rows; i++)
    {
        delete[] data[i];
    }
    delete[] data;
    data = nullptr;
}

#endif

其中,当我在 main() function 调用赋值运算符时

CMatrix<double> t1(2);
CMatrix<double> t2(3);
t1 = t2;

当赋值运算符一遍又一遍地调用自己并且永远不会返回时,堆栈最终会出现分段错误,为什么? 经过长时间的调试,我不知道是什么原因造成的。 这不是教科书的三法则吗? 我正在使用 const 引用并从分配中返回引用。 是的,我知道我可以使用 std:: 等,问题是在我知道之后在 Cuda 中是不可能的。

赋值运算符 CMatrix::operator= 递归调用自身。

template <class T>
__host__ __device__ CMatrix<T>& CMatrix<T>::operator=(
    const CMatrix<T> &rhs                                       // In: Right hand side matrix/vector to assign
    )
{
    ...

    return *this = CMatrix<T>(rhs);  // calls operator=
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM