m_pGladiator
m_pGladiator

Reputation: 8620

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

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 array

int *array = new int[size];

the delete looks like:

delete [] array;

That's great. But if we have two dimension array, we can not do

delete [][] twoDimenstionalArray;

Instead, we should loop and delete the items, like in this example.

Can anybody explain why?

Upvotes: 23

Views: 18644

Answers (9)

user27732
user27732

Reputation: 23

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).

Upvotes: 0

Adam
Adam

Reputation: 2061

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.

Upvotes: 3

Greg Rogers
Greg Rogers

Reputation: 36439

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.

Upvotes: 20

INS
INS

Reputation: 10820

You can use a wrapper class to do all those things for you. Working with "primitive" data types usually is not a good solution (the arrays should be encapsulated in a class). For example std::vector is a very good example that does this.

Delete should be called exactly how many times new is called. Because you cannot call "a = new X[a][b]" you cannot also call "delete [][]a".

Technically it's a good design decision preventing the appearance of weird initialization of an entire n-dimensional matrix.

Upvotes: 0

James Rose
James Rose

Reputation: 255

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).

Upvotes: 6

KPexEA
KPexEA

Reputation: 16778

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.

Upvotes: 7

Dan Goldstein
Dan Goldstein

Reputation: 23662

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.

Upvotes: 25

shsteimer
shsteimer

Reputation: 28810

not sure of the exact reason from a language design perspective, I' guessing it has something to do with that fact that when allocating memory you are creating an array of arrays and each one needs to be deleted.

int ** mArr = new int*[10];
for(int i=0;i<10;i++)
{
   mArr[i]=new int[10];
}

my c++ is rusty, I'm not sure if thats syntactically correct, but I think its close.

Upvotes: 4

Dan Hewett
Dan Hewett

Reputation: 2270

delete[] applies to any non-scalar (array).

Upvotes: 0

Related Questions