4

If you have said

int *arr = new int[5];

What is the difference between

delete arr;

and

delete [] arr;

I ask this because I was trying to deallocate memory of a 2d array and

delete [][] arr; 

did not seem to work but

delete arr;

seemed to work fine

Thank you in advance!

AronAtVW
  • 115
  • 1
  • 2
  • 11
  • 1
    That's an automatic or static array. In either case, invoking `delete` or `delete[]` is *undefined behavior*. So the "difference" is irrelevant. *Neither* would be correct. Does your book/tutorial seriously point you in that direction? Because if so, get a different one. [Related question here](https://stackoverflow.com/questions/4255598/delete-vs-delete). – WhozCraig Mar 02 '17 at 04:18
  • sorry I meant arr to be a pointer. But yes my textbook suggest we use delete [] arr for an array. – AronAtVW Mar 02 '17 at 04:40
  • On a *dynamic* array, certainly. On an automatic array, *no*. The short is, if you `new` you `delete`, if you `new [n]` you `delete[]`. That's it. – WhozCraig Mar 02 '17 at 04:40
  • Understood I misunderstood the brackets for dimensions in an array,. So delete [] array would be good practice for a 2d array as well? – AronAtVW Mar 02 '17 at 04:47
  • Again, if you used array-new then use array delete. If the "thing" you have was allocated with some form of `new []`, then `delete []` is to be used. And frankly, *none* of this is ideal [in modern c++ anyway](https://dl.dropboxusercontent.com/u/6101039/Modern%20C%2B%2B.pdf). – WhozCraig Mar 02 '17 at 04:51

4 Answers4

5

new type requires delete
new type[size] requires delete []
Using one instead of the other is wrong.

Btw you should not use such raw pointers like this unless you have a very good reason. Use std::vector or std::array instead.

And 2D MxN arrays should generally be linearised into 1D M*N arrays, also using these containers.

O'Neil
  • 3,790
  • 4
  • 16
  • 30
2

If you have said

int arr[5];

What is the difference between

delete arr;

and

delete [] arr;

One has an extra pair of brackets in it. Both will probably crash and/or corrupt the heap. This is because arr is a local variable which can't be deleted - delete only works on things allocated with new.

delete [][] arr; is not valid syntax. For an array allocated with for example new int[2][2], use delete [].

Community
  • 1
  • 1
user253751
  • 57,427
  • 7
  • 48
  • 90
1

Neither of the delete's is correct.

When you declare an array like this:

int arr[5];

The space is allocated on the stack. Memory allocated on the stack isn't cleaned by delete. It gets auto cleaned (Although clean is not the correct term technically) when the stack unrolls on exit of scope. (Check When is an object "out of scope"? )

If you declre your array like this:

int *arr = new int[5]; // new allocates memory on heap

You call

delete[] arr; // this takes care of cleaning up memmory on **heap**
Community
  • 1
  • 1
bashrc
  • 4,725
  • 1
  • 22
  • 49
0

I assume you mean new int[5], and the same new for dynamic memory. Otherwise, you are using stack, not the heap, and delete is undefined

While delete arr may seem to work for a 2-d array, I believe that the standard requires the following:

delete [] arr
arr=nullptr

This is because memory allocated with new [] must be freed with delete [] and vice versa. Also, it is dangerous to leave dangling pointers, hence the final line

パスカル
  • 479
  • 4
  • 13