Search code examples
c++mallocnew-operator

Can I mix new and malloc on different redirection?


Is this behaviour undefined where I am mixing both new and 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
}

Solution

  • Yes, you can do that. You must call delete[], delete and free accordingly later on. Be careful to not free something you got from malloc with delete etc.