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
}
InQusitive
  • 5,256
  • 2
  • 24
  • 60

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.

Ayxan Haqverdili
  • 23,309
  • 5
  • 37
  • 74
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
  • 223,800
  • 12
  • 181
  • 301