我可以在不同的重定向上混合使用 new 和 malloc 吗?
Can I mix new and malloc on different redirection?
我混合使用 new
和 malloc
时,这种行为是否未定义?
int main()
{
int ***arr = new int**[1];
arr[0] = static_cast<int**>(malloc(sizeof(int**)));
arr[0][0] = new int;
arr[0][0][0] = 1;
//now, release memory using appropriate operator
}
是的,你可以做到。您必须稍后相应地调用 delete[]
、delete
和 free
。小心不要释放你从 malloc
和 delete
等
得到的东西
Is this behaviour undefined where I am mixing both new and malloc?
示例中没有UB
你可以这样做,但没有任何好处。
我混合使用 new
和 malloc
时,这种行为是否未定义?
int main()
{
int ***arr = new int**[1];
arr[0] = static_cast<int**>(malloc(sizeof(int**)));
arr[0][0] = new int;
arr[0][0][0] = 1;
//now, release memory using appropriate operator
}
是的,你可以做到。您必须稍后相应地调用 delete[]
、delete
和 free
。小心不要释放你从 malloc
和 delete
等
Is this behaviour undefined where I am mixing both new and malloc?
示例中没有UB
你可以这样做,但没有任何好处。