c++ - C++ 中的语句 new employee *[num] 是什么意思

标签 c++ arrays pointers new-operator

我在一本书中看到了这个说法。

new employee *[num];

employee 已经声明为 struct employee num 是一个整数;

这在语句中 = 运算符的右侧。

那么,这句话是什么意思呢? 书中没有对上述说法作任何解释。

最佳答案

这将分配一个所需的内存来保存 num 个指向免费存储上的 employee 的指针。

例如:

employee** a = new employee* [2]; // 2 pointers on the heap

堆:

address a (e.g. 0x97a0008): pointer1 to employee

address a + 1 ( 0x97a000c): pointer2 to employee

旁注:您在数组上使用 delete[],因此您可以使用 delete[] a 删除上面的内容 但是 您将不得不循环如果您已经为它们分配了内存,则首先遍历所有条目,因此必须在松开指针之前释放它。

https://stackoverflow.com/a/13477214/1141471

关于c++ - C++ 中的语句 new employee *[num] 是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24350811/

相关文章:

c++ - 从具有非虚拟析构函数的类中私有(private)继承是否安全?

c++ - LinkedList ADT指针 block

c# - 从对象数组中删除重复项

python - 如何在 numpy 中对 3 个向量进行外积来创建 3d 矩阵? (与 nd 相同)

arrays - 为什么 Range#select 会产生一个 Array 对象?

c - 值返回错误值

c - 将数组/指针传递给c中的结构

c++ - ',' 或 ';' 之前的预期类名

c++ - 为什么这段代码在到达管道时停止运行?

c++ - CL_DEVICE... 参数的含义