1

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
}
Sreeraj Chundayil
  • 5,548
  • 3
  • 29
  • 68

2 Answers2

4

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.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
1

Is this behaviour undefined where I am mixing both new and malloc?

There is no UB in the example.

You can do this, but there is no benefit.

eerorika
  • 232,697
  • 12
  • 197
  • 326