Which delete format to use?

When do you use delete [] p vs. delete p? Is there a big difference..?
Last edited on
Yes there's a big difference.

delete[] p; should be used when p was allocated wtih new[].

delete p; should be used when p was allocated with new.

Example:

1
2
3
4
5
6
int* p = new int[10];  // new[] used;
delete[] p;  // so use delete[]


p = new int;  // new used
delete p;  // so use delete 
Alright thank you.

So in the second case you're saying p is a pointer, but doesn't allocate a specific size...Am I reading that right?
In the first case, we allocate contiguous space for 10 ints.
In the second, we allocate contiguous space for 1 int.
The difference between delete and delete[] is visible when you have allocated objects with non-trivial destructors.
delete would call only one destructor
delete[] would call the destructor of every object allocated with new[]
Topic archived. No new replies allowed.