[英]C++ read memory faild
I have 2 classes:我有 2 节课:
class b_class
{
public:
int time_viewed;
int parent_course;
b_class()=default;
b_class(int parent_course):time_viewed(0), parent_course(parent_course){
}
};
and:和:
class b_course
{
public:
int course_id;
int num_of_classes;
b_class **classes;
b_course(int course_id,int num_of_classes);
};
In the latter I wrote the following code:在后者中,我编写了以下代码:
b_course::b_course(int course_id,int num_of_classes) {
this->course_id=course_id;
this->num_of_classes=num_of_classes;
classes=new b_class*[num_of_classes*sizeof(b_class*)];
for (int i=0;i<num_of_classes;i++)
{
classes[i]->time_viewed=0;
//classes[i]->parent_course=course_id;
}
}
But I'm getting an error because I am trying to access some memory which shouldn't be accessed.但是我遇到了一个错误,因为我正在尝试访问一些不应访问的 memory。
read memory from 0x7000000000000000 failed (0 of 4 bytes read)从 0x7000000000000000 读取 memory 失败(读取 4 个字节中的 0 个)
anyone know what is the reason for this?有谁知道这是什么原因?
classes
is an array of pointers to b_class
classes
是指向b_class
的指针数组
You are allocating an array of pointers that don't point at any valid objects.您正在分配一个不指向任何有效对象的指针数组。 And you are allocating more space for the array then you actually need.而且您为数组分配了比实际需要更多的空间。
b_course::b_course(int course_id,int num_of_classes) {
this->course_id = course_id;
this->num_of_classes = num_of_classes;
classes = new b_class*[num_of_classes]; // <- get rid of sizeof() here
for (int i = 0; i < num_of_classes; i++)
{
classes[i] = new b_class(/*course_id*/); // <- add this
classes[i]->time_viewed = 0;
}
}
That being said, a better option is to allocate an array of objects instead of an array of pointers, eg:话虽如此,更好的选择是分配对象数组而不是指针数组,例如:
class b_course
{
public:
int course_id;
int num_of_classes;
b_class *classes;
b_course(int course_id, int num_of_classes);
};
b_course::b_course(int course_id, int num_of_classes) {
this->course_id = course_id;
this->num_of_classes = num_of_classes;
classes = new b_class[num_of_classes];
for (int i = 0; i < num_of_classes; i++)
{
classes[i].time_viewed = 0;
//classes[i].parent_course = course_id;
}
}
Either way, since you are allocating objects dynamically, be sure to follow the Rule of 3/5/0 by also implementing a destructor to free the objects and array, and a copy constructor and copy assignment operator to make copies of the array and objects from one b_course
to another.无论哪种方式,由于您是动态分配对象,因此请务必遵循3/5/0 规则,同时实现析构函数来释放对象和数组,以及复制构造函数和复制赋值运算符来制作数组和对象的副本从一个b_course
到另一个。
The best way to handle all of this is to use a std::vector
instead of new[]
, and let the compile handle the details for you, eg:处理所有这些的最佳方法是使用std::vector
而不是new[]
,并让编译为您处理细节,例如:
#include <vector>
class b_course
{
public:
int course_id;
std::vector<b_class> classes;
b_course(int course_id,int num_of_classes);
};
b_course::b_course(int course_id,int num_of_classes) {
this->course_id=course_id;
this->classes.resize(num_of_classes);
for (int i=0;i<num_of_classes;i++)
{
classes[i].time_viewed=0;
//classes[i].parent_course=course_id;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.