无法打印出 obj 成员变量C++

Unable to print out C++ obj member variable

本文关键字:成员 变量 C++ obj 打印      更新时间:2023-10-16

我正在尝试学习一些OOP实践,同时为我的研究开发模型。 我似乎遇到了一些问题,我不确定这些问题是否属于我正在使用的书的范围,斯蒂芬·普拉塔(Stephen Prata)的C++入门。

现在,我正在尝试开发一个名为随机模型的对象。 这个 SM 对象应该能够接受另一个称为 LengthDistribution 的对象并对其运行模拟。 我目前正在为这种类型的交互设置框架,使用以下伪代码。 作为警告,我将类定义组合在一起,因为我最初将它们分别放在两个文件中。 您还将在其中看到一些CudaMallocManaged(),因为我也打算在GPU上使用这些对象,但是CUDA不是这里的问题。

int main()
{
//Read initial conditions, maxlength, maxiterations from input
//Create a length distribution object from IC's and length
LengthDistribution* LD = new LengthDistribution(initialconditions, maxlength)
//Create a model object to operate on LD maxiterations number of times.
StochasticModel* SM = new StochasticModel(LD, maxiterations)
//Test that I've created an LD object as I expected by printing its values out.
LD -> h_printToScreen(); //<- Works fine!
//Test that model has a correct version of LD by printing LD's information to screen through 
//SM's print to screen function.
SM->printToScreen(); //<- But when I have an object that contains the object call a function that calls its function (inception), memory access violations occur.
}

我的长度分布类。

class LengthDistribution : public Managed
{
private:
    int m_maxlength;
    int* m_lengthDistribution;
public:
    //User defined constructor.
    LengthDistribution(int* initialconditions, int maxlength)
    {
    m_maxlength = maxlength;
    m_lengthDistribution = new int[maxlength];
    m_lengthDistribution = initialconditions; 
    }
    //Default constructor
    //Default destructor
    //Unified memory copy constructor allows pass-by-value
    LengthDistribution::LengthDistribution(const LengthDistribution &LD)
    {
    //Copy maxlength to new state.
    m_maxlength = LD.m_maxlength;
    //Allocate CUDA Managed memory for lengthDistribution array
    cudaMallocManaged(&m_lengthDistribution, m_maxlength);
    //Copy array to new state.
    memcpy(m_lengthDistribution, LD.m_lengthDistribution, m_maxlength);
    }
    __host__ void h_printToScreen()
    {
        printf("Host maxlength: ");
        std::cout<<m_maxlength; 
        std::cout<<"n";
        printf("Host length distribution: ");
        for (int i = 0; i < m_maxlength; i++)
            std::cout<<m_lengthDistribution[i];
        printf("n");
    }
}

我的随机模型类

class StochasticModel : public Managed
{
private : 
    int m_numberOfIterations;
    LengthDistribution* state;
public:
    //User defined constructor
    StochasticModel(LengthDistribution* LD, int numberOfIterations)
    {
        //Copy desired number of iterations.
        m_numberOfIterations = numberOfIterations;
        //Assign LD to SM's state variable.  I think I'm having an issue here.
        //Copy LD into SM object's state variable.
        LengthDistribution* state = new LengthDistribution(*LD);
    }
    //User defined copy constructor
    Stochastic::Model(const StochasticModel &SM)
    {   
        m_numberOfIterations = SM.m_numberOfIterations;
        cudaMallocManaged(&state, sizeof(SM.state));
        state = new LengthDistribution(*SM.state);
        //memcpy(state, SM.state, sizeof(state));
    }
    //Print out member length distribution's values.
    printToScreen()
    {
    state->h_printToScreen();  //When I trace the debugger through here, it triggers an access violation when trying to print out the state's array.
    }
}

这是其他两个类继承自的托管类。 其目的是允许将来移植到图形卡。

class Managed {
public:
  void *operator new(size_t len) {
    void *ptr;
    cudaMallocManaged(&ptr, len);
    return ptr;
  }
  void operator delete(void *ptr) {
    cudaFree(ptr);
  }
};

最终结果是应用程序在运行后"已停止工作"。 它编译正常,但是当它遇到状态<-printToScreen调用的内存访问错误时,它会"停止工作"。 通过调试器,它说它尝试从中打印的 LD 对象和整数的数组未定义。 我觉得我在这里缺少一些基本的东西,但我希望能够使用 SM 对象来操作和显示来自 LD 对象的信息。

在构造函数中,您创建了一个新的局部变量,用于隐藏成员变量state。这使得成员变量未定义,稍后当您尝试访问它时会导致崩溃。

您应该改用初始值设定项列表:

StochasticModel(LengthDistribution* LD, int numberOfIterations) :
    m_numberOfIterations(numberOfIterations),
    state(new LengthDistribution(*LD))
{
}