在我同时混合 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
}
答案 0 :(得分:4)
是的,你可以这样做。稍后您必须相应地调用 delete[]
、delete
和 free
。小心不要用 malloc
等释放你从 delete
得到的东西
答案 1 :(得分:1)
在混合 new 和 malloc 的情况下,这种行为是否未定义?
示例中没有 UB。
你可以这样做,但没有任何好处。