假设我有一个结构数组,其中又包含动态分配的数组,delete[] deep 会删除我上面所说的所有内容吗?
例如,假设我有这样的结构:
struct A
{
char* name;
float* data;
int** image;
};
我创建的代码中的某个地方
A** array = new A[n];
然后我通过以下方式为数组中的所有结构分配内存
for(int i=0; i<n; i++)
{
array[i] = new A;
}
然后在其他地方类似地填充结构的内容(使用 malloc/new 创建名称、数据和图像)。
现在如果我说 delete[] array;
,到目前为止分配的所有内存(对于 char*、float*、int**、structure 和数组)是否会被销毁?
最佳答案
没有。
如果你想要更自动的内存管理,使用 vector 。
struct A {
string name;
vector<float> data;
vector<vector<int>> image;
}
关于c++ - delete[ ] 会深度删除 C++ 中的结构吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24751172/