Why "delete [][]... multiDimensionalArray;" operator in C++ does not exist Why "delete [][]... multiDimensionalArray;" operator in C++ does not exist arrays arrays

Why "delete [][]... multiDimensionalArray;" operator in C++ does not exist


Technically, there aren't two dimensional arrays in C++. What you're using as a two dimensional array is a one dimensional array with each element being a one dimensional array. Since it doesn't technically exist, C++ can't delete it.


Because there is no way to call

int **array = new int[dim1][dim2];

All news/deletes must be balanced, so there's no point to a delete [][] operator.

new int[dim1][dim2] returns a pointer to an array of size dim1 of type int[dim2]. So dim2 must be a compile time constant. This is similar to allocating multi-dimensional arrays on the stack.


The reason delete is called multiple times in that example is because new is called multiple times too. Delete must be called for each new.

For example if I allocate 1,000,000 bytes of memory I cannot later delete the entries from 200,000 - 300,00, it was allocated as one whole chunk and must be freed as one whole chunk.