C++ 重新分配时向量调用析构函数
我有一个指向类的动态分配的3D指针数组:C++ 重新分配时向量调用析构函数,c++,arrays,pointers,dynamic,C++,Arrays,Pointers,Dynamic,我有一个指向类的动态分配的3D指针数组: class Foo { public: int a; float b; float c; Foo(int x, float y, float z) { a = x; b = y; c = z; } }; 在课堂上: class Bar { public: Foo ****p_arr; Bar(); ~Bar(); void Create(); }; 这样分配(在Bar::Create(
class Foo {
public:
int a;
float b;
float c;
Foo(int x, float y, float z) { a = x; b = y; c = z; }
};
在课堂上:
class Bar {
public:
Foo ****p_arr;
Bar();
~Bar();
void Create();
};
这样分配(在Bar::Create()
中):
我有一个std::vector
的Bar
s,当我push_back
到vector新项时,vector正在重新分配并调用析构函数。当我再次访问p_arr
时,它被释放,程序在析构函数中崩溃。
它说:
它在这里崩溃:
if (p_arr[i][j][k] != nullptr) // <- here
delete p_arr[i][j][k];
如果(p_arr[i][j][k]!=nullptr)/我刚刚将复制构造函数添加到条中,代码运行良好。您可以使用一维数组存储N维数组,只要N-1维具有相同的固定大小,就像您这里的一样。这样,它只需要1个内存分配,提高了内存利用率和局部性,消除了所有不必要的复杂性
例如:
#include <memory>
struct Foo {
int a = 0;
float b = 0;
float c = 0;
};
int constexpr ARR_SIZE = 10;
Foo& at(std::unique_ptr<Foo[]> const& a3d, unsigned i, unsigned j, unsigned k) {
return a3d[(i * ARR_SIZE + j) * ARR_SIZE + k];
}
int main () {
std::unique_ptr<Foo[]> a3d{new Foo[ARR_SIZE * ARR_SIZE * ARR_SIZE]};
at(a3d, 0, 0, 0) = {1,2,3};
}
#包括
结构Foo{
int a=0;
浮动b=0;
浮点数c=0;
};
int constexpr ARR_SIZE=10;
Foo&at(std::unique_ptr const&a3d、无符号i、无符号j、无符号k){
返回a3d[(i*ARR_SIZE+j)*ARR_SIZE+k];
}
int main(){
std::unique_ptr a3d{new Foo[ARR_SIZE*ARR_SIZE*ARR_SIZE]};
at(a3d,0,0,0)={1,2,3};
}
您可以使用std::unique\u ptr
而不是std::vector
——后者是可复制的。该//初始化p\u arr[i][j][k]
代码背后隐藏着什么?p_arr[i][j][k]
是如何初始化的?您有4个delete
s,但只有3个new
s?如果我可以问的话,为什么您有一个3D指针数组?我希望这是一个学术练习。你试过运行这个特定的测试代码吗?它似乎应该正确编译。遗憾的是,我现在不在电脑前,无法进行测试。是否有任何Foo指针可能被删除两次?
0xC0000005: Access violation reading location 0xFFFFFFFF.
if (p_arr[i][j][k] != nullptr) // <- here
delete p_arr[i][j][k];
#include <memory>
struct Foo {
int a = 0;
float b = 0;
float c = 0;
};
int constexpr ARR_SIZE = 10;
Foo& at(std::unique_ptr<Foo[]> const& a3d, unsigned i, unsigned j, unsigned k) {
return a3d[(i * ARR_SIZE + j) * ARR_SIZE + k];
}
int main () {
std::unique_ptr<Foo[]> a3d{new Foo[ARR_SIZE * ARR_SIZE * ARR_SIZE]};
at(a3d, 0, 0, 0) = {1,2,3};
}