Reputation: 101
I am not sure if both of these works to delete:
p = new int[1];
delete p
and
delete [] p;
If both works, what is the difference between the above two delete
s?
Upvotes: 2
Views: 767
Reputation: 6581
delete p
deletes only one element. delete [] p
deletes an array. If you use new type[]
, then use delete []
.
If you're new
has a []
, then delete
has a []
.
By the way there has to be an exact dupliate of this somewhere.
Upvotes: 1
Reputation: 2411
They are not the same. Even though delete p
would compile but could potentially cause problems at runtime.
delete p
will invoke operator delete(void*)
where as delete []
will call operator delete[](void*)
. The default behavior is for delete[]
to call delete
but if a custom operator for delete []
has been implemented, it won't be called if you just call delete
and you'll have a problem (probably a silent one).
Good rule of thumb is if you used []
in the new
, use it in the delete
.
Upvotes: 2
Reputation: 137770
An array of one instance is still an array. There is simply not a special case, so you need to treat it as an array and not a single object. Non-array delete
may crash.
An array of objects may be preceded by a number signifying the size of the array. If that's the case, delete[]
, not delete
, is smart enough to pass the correct pointer to free
.
Upvotes: 4