How do I delete this 2D array in c++
Solution 1
Does the above de-allocation delete the array?
Yes it does.
Simply follow the rule:
You need to call delete
or delete []
as many times you called new
or new []
respectively.
If you had an array of pointers where each index was allocated dynamic memory, you would need to explicitly loop through it and deallocate each array element explicitly.
On a side note you are much better off using a std::vector
or std::array
rather than dynamically allocated array.
Solution 2
Yes it does. The explanation is because there is no information in memory about how much dimensions your array has.
If you have a 3*4 items array, it's exactly the same as a 12 items array. It's just the way you address the specific elements (which are stored in a line after line fashion) that changes.
So delete[]
will work perfectly.
But using new
and delete
operators is not so common these days with smart pointers, unless you really have a good reason to control allocation yourself.
Solution 3
new float[10][3]
allocates an array of 10 arrays of 3 floats
and returns a pointer to the first element (which is an array of 3 floats).
Calling delete[]
on this pointer causes the whole array to be deleted.
The only difference between the two cases is that in the first case, the type of the values stored in the dynamically allocated array is float
and in the second case the type is float[3]
.
Anubha
Updated on August 12, 2020Comments
-
Anubha almost 4 years
In simple 1D array:
node *nodes = new node[MAX_NODES];
Deleting by:
delete [] nodes;
Deletes all the nodes allocated in the array.
But in this case:
float (*buildingArray)[3] = new float[10][3];
Does this statement make
buildingArray
a single dimension array of 3 float pointers? And this is the deallocation line:delete[] buildingArray;
Does the above deallocation
delete
the array, but I am doubtful about whether it will delete its references? -
Anubha about 11 yearsokay so here each element is not having dynamic memory, only 1 dimension (rows) have dynamic memory and the 3 columns have static memory, so no need to de -allocate them. Am I right ?
-
Alok Save about 11 years@Anubha: The statement
float (*buildingArray)[3] = new float[10][3];
allocates enough dynamic memory for storing an array of 10 arrays of 3 floats. The entire storage is on freestore(aka heap). But since it was allocated using a single call tonew []
a single call todelete []
dellocates it perfectly. -
Anubha about 11 yearsok, so in this question stackoverflow.com/questions/936687/…, the array initailisation was using 2 new statements, therfore simple delete[] ary ; would not have worked in that case ?
-
Alok Save about 11 years@Anubha: Yes. In the Q, user wanted to create a 2 dimensional array which is nothing but array of pointer to arrays. A pointer by itself is not very helpful it needs to point to valid memory to be usable. Since each element is a pointer in array of pointers, same is the case with an array of pointers. Each element must be allocated memory separately and hence the need for deallocation separately by calling
delete
. The rule I stated holds good, Just remember the rule and you won't be tripping. -
Anubha about 11 yearsso my array, is it 2 dimensional?
-
Alok Save about 11 years@Anubha: No it is an pointer to an array. I do not mean any offence but since you are starting to learn a good book should do you a truckload of goods. I do not know what books your curriculum mandates but you should pick up one from the link i provided.