tags:

views:

1710

answers:

8

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?

A: 

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

Dan Hewett
No, you'll cause a memory leak if you try to delete[] a jagged array.
Jacob
+2  A: 

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.

shsteimer
+15  A: 

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.

Dan Goldstein
Who cares if it's correct. It was accepted.
Windows programmer
+2  A: 

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.

KPexEA
Therefore the opposit is also true, if it was allocated as multiple chunks it must be freed as multiple chunks
Binary Worrier
Yes, my point exactly!
KPexEA
+13  A: 

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.

Greg Rogers
it's the other way around. dim2 must be a compile time constant, while dim1 doesn't need not to be, and a int(*)[dim2] is returned :) you can memorize the syntax when imagine what happens if you do int v[dim1][dim2]; and check the type of v after decaying to a pointer: it is int(*)[dim2];
Johannes Schaub - litb
+3  A: 

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

James Rose
A: 

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.

Iulian Şerbănoiu
A: 

well, i think it is easy to implement, but too danger. it is easy to tell whether a pointer is created by new[], but hard to tell about new[]....