我可以在不同的重定向上混合使用 new 和 malloc 吗?

时间:2021-08-01 16:48:13

标签: c++ malloc new-operator

在我同时混合 newmalloc 的情况下,这种行为是否未定义?

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
}

2 个答案:

答案 0 :(得分:4)

是的,你可以这样做。稍后您必须相应地调用 delete[]deletefree。小心不要用 malloc 等释放你从 delete 得到的东西

答案 1 :(得分:1)

<块引用>

在混合 new 和 malloc 的情况下,这种行为是否未定义?

示例中没有 UB。

你可以这样做,但没有任何好处。

相关问题