[英]Using vectors to initialize object arrays for parameterized constructors
I am working with Visual Studio 2010 and am relatively new to C++.我正在使用 Visual Studio 2010 并且对 C++ 比较陌生。 The program I am trying to work with has a class with a parameterized constructor and a destructor in its declaration.
我正在尝试使用的程序在其声明中有一个带有参数化构造函数和一个析构函数的类。 Somewhere in the listing, there was a dynamic object array creation using 'new'.
在清单的某个地方,有一个使用“new”的动态对象数组创建。 However, I faced issues as object array initialization for parameterized constructors is not possible.
但是,我遇到了无法为参数化构造函数初始化对象数组的问题。
I have, thus, tried to implement vectors:因此,我试图实现向量:
std::vector< class_type > my_object_array(length, arg);
std::vector<class_type> my_object_array(length, arg); //current attempt
//当前尝试
my_object_array = new class_type[length](arg);
my_object_array = new class_type[length](arg); //previous code
//之前的代码
However, once this object array is created, a ~vector destructor is called and I receive a runtime error of "Debug Assertion Failed... _BLOCK_TYPE_IS_VALID (pHead->nBlockUse)"但是,一旦创建了此对象数组,就会调用 ~vector 析构函数,并且我收到“调试断言失败... _BLOCK_TYPE_IS_VALID (pHead->nBlockUse)”的运行时错误
Based on previous such questions on SO, I think this is because of double deletion but I have not called the destructor explicitly during the debugging steps and I still receive this error.基于之前关于 SO 的此类问题,我认为这是因为双重删除,但我没有在调试步骤中明确调用析构函数,我仍然收到此错误。
Any help is appreciated!任何帮助表示赞赏! Thanks!
谢谢!
Edit: Added some snippets of the code with names changed.编辑:添加了一些更改名称的代码片段。
class class_type {
public:
class_type(int var1);
~Class_type();
/*
Member functions
*/
private:
int var1;
double var2;
double length;
double width;
double* arr1;
};
Constructor definition:构造函数定义:
Class_type::Class_type(int il){
length = 0;
width = 0;
var1 = il;
var2 = 5;
arr1 = new double[5];
}
Destructor definition:析构函数定义:
Class_type::~Class_type(){
delete [] arr1;}
Code where error occurs:发生错误的代码:
int class_type_2::create_my_objects(int num_elem){
input_value = 10;
if ( num_elem == 0 ) {
std::cout<<"Warning!"<<endl;
} else {
std::vector<class_type> my_object_array(num_elem, input_value);
//my_object_array= new class_type[num_elem](input_value);
} //Debugger doesn't go beyond this step!
return 0;
}
std::vector my_object_array(length, arg); //current attempt
Your vector might be out of scope as much as you wrote is just one line.您的向量可能超出范围,因为您写的只是一行。 So, it's not possible to reply exact cause what happened to yours.
因此,无法回答您发生的事情的确切原因。 I think you might pass the vector to another copy constructor or move constructor where it deletes the memory and calls the destructor.
我认为您可以将向量传递给另一个复制构造函数或移动构造函数,在那里它删除内存并调用析构函数。
As @Matthieu Brucher just said you should manage memory by yourself if allocated using "new".正如@Matthieu Brucher 刚才所说,如果使用“new”分配内存,您应该自己管理内存。 Otherwise double deletion is a common issue and may other memory issues may occur.
否则双重删除是一个常见问题,可能会出现其他内存问题。
Edit1: I just updated after seeing your code Edit1:我看到你的代码后才更新
std::vector<class_type> my_object_array(num_elem, input_value);
} ---> when this scope hits it will release all stacked memory inside it and call their object constructors
Initialize the raw pointer with nullptr.用 nullptr 初始化原始指针。
double* arr1; // you have written
double* arr1 = nullptr; // recommended
Edit 2:编辑2:
you can delete a memory if allocated with malloc() or calloc().如果使用 malloc() 或 calloc() 分配内存,则可以删除内存。 otherwise use vector.
否则使用向量。 it will manage your memory without leak.
它将管理您的内存而不会泄漏。
If you use a std::unique_ptr
for your object instead of the double*
, you will see that your code doesn't compile anymore.如果您为对象使用
std::unique_ptr
而不是double*
,您将看到您的代码不再编译。 This indicates that the code you have uses the default copy constructor, and the pointer will be copied to a copied class.这表明您拥有的代码使用默认的复制构造函数,并且指针将被复制到复制的类。
Unfortunately, this means that several objects will have the same pointer and try to free it, which will fail.不幸的是,这意味着多个对象将具有相同的指针并尝试释放它,这将失败。 If you follow good c++ practices and never call
new
and delete
yourself, you won't see these issues anymore, because they will force you to think properly from the start.如果您遵循良好的 c++ 实践并且从不自己调用
new
和delete
,您将不会再看到这些问题,因为它们会迫使您从一开始就正确思考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.