I was always wondering if there is operator for deleting multi dimensional arrays in the standard C++ language.
If we have created a pointer to a single dimensional arra
delete[] applies to any non-scalar (array).
The reason you have to loop, like in the example you mention, is that the number of arrays that needs to be deleted is not known to the compiler / allocator.
When you allocated your two-dimensional array, you really created N one-dimensional arrays. Now each of those have to be deleted, but the system does not know how many of them there are. The size of the top-level array, i.e. the array of pointers to your second-level arrays, is just like any other array in C: its size is not stored by the system.
Therefore, there is no way to implement delete [][]
as you describe (without changing the language significantly).
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.
While all these answers are relevant, I will try to explain what came to an expectation, that something like delete[][] array;
may work on dynamically allocated arrays and why it's not possible:
The syntax int array[ROWS][COLS];
allowed on statically allocated arrays is just abstraction for programmers, which in reality creates one-dimensional array int array[ROWS*COLS];
. But during compilation process (when dimension sizes COLS
and ROWS
must be constants by standard) the compiler also remembers the size of those dimensions, that are necessary to later address elements using syntax e.g. array[x][y] = 45
. Compiler, being known of this size, will then replace [x][y]
with the corresponding index to one-dimensional array using simple math: [COLS*x + y]
.
On the other hand, this is not the case with dynamically allocated arrays, if you want the same multi-dimensional functionality (in fact notation). As their size can be determined during runtime, they would have to remember the size of each additional dimension for later usage as well - and remember that for the whole life of the array. Moreover, system changes would have to be implemented here to work with arrays actually as multi-dimensional, leaving the form of [x][y]
access notation in the code, not replacing it with an one-dimensional notation during compilation, but later replacing it within runtime.
Therefore an absence of array = new int[ROWS][COLS]
implies no necessity for delete[][] array;
. And as already mentioned, it can't be used on your example to delete your "multi-dimensional" array, because your sub-arrays (additional dimensions) are allocated separately (using separate new
call), so they are independent of the top array (array_2D
) which contains them and they all can't be deleted at once.
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.
Well, I think it is easy to implement, but too dangerous. It is easy to tell whether a pointer is created by new[]
, but hard to tell about new[]...[]
(if allowed).