c++ - delete[ ] 会深度删除 C++ 中的结构吗?

标签 c++ arrays memory-management struct

假设我有一个结构数组,其中又包含动态分配的数组,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/

相关文章:

c++ - 在 Clang 上禁用 "deletion of copy constructor when move constructor is avaliable"

python - 理解 IPython 中的 numpy.linalg.norm()

memory-management - Linux 内核中的 Slab 和 Slub 分配器如何选择?

Java Runtime.getRuntime().exec() 替代品

c++ - 位移时有n位的限制吗?

c++ - 在 C 中使用具有虚拟成员(即非 POD)的 C++ 结构

c++ - 我使用 AudioKit 使用什么语言?

javascript - React Hooks : Shuffle array immediately on load, 和 onClick

delphi - 是否可以在 Delphi 和 FreePascal 中声明数组的 const 而不让元素为常量?

c++ - 如果 "delete this"不需要访问 "somethings",那么在 "this"之后做某事是否安全?