无法打印出 obj 成员变量C++
Unable to print out C++ obj member variable
我正在尝试学习一些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))
{
}
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 将包含C样式数组的对象初始化为成员变量(C++)
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 在循环中按顺序遍历成员变量
- c++类声明时,相同的例程,不同的成员变量类型
- 如何从另一个文件继承私有成员变量和公共函数
- 在C++类中,是否必须初始化作为数组的成员变量
- 如何从子成员函数修改父公共成员变量
- 我可以在 C++ 中将数据成员/变量从其定义之外添加到结构中吗?
- 从私有成员变量的成员方法返回unique_ptr
- 在派生类中使用基类的私有成员变量的最佳方法
- 静态 constexpr 类成员变量对多线程读取是否安全?
- C++:是否可以使用非静态成员变量模板?
- 打印所有继承的类成员变量和方法
- 如何在复杂继承中访问静态成员变量
- 为什么我不能在返回 const 的布尔函数中为类成员变量赋值?C++
- 成员变量与函数概念检查