【发布时间】:2014-08-08 19:52:01
【问题描述】:
我有这个结构:
struct problem
{
int length;
struct node **x;
};
我创建了一个这样的结构:
struct problem prob;
我可以在 C 中做到这一点:
prob.x = Malloc(struct node *,prob.length);
但是我如何使用 new 以 c++ 风格做到这一点?为什么?
【问题讨论】:
-
向我们展示失败的代码。
-
Malloc是你自己的函数吗?malloc仅适用于 1 个参数 -
prob.x = new node*[prob.length]呢? -
“……为什么?”好问题。将代码保留为 C,或将其更改为适当的 C++,例如:
struct problem { std::vector<std::unique_ptr<node>> x; }; -
把它放在问题中。
标签: c++ memory-management malloc