Is it safe to delete[] of NULL?

1
2
int *p = NULL;
delete[] p;
Nope, I've hard crashes caused by this.

use:
if (p) delete[] p;
OK. Thanks.
It is entirely safe to use delete or delete[] on a null pointer.

Stewbond wrote:
Nope, I've hard crashes caused by this.

What compiler are you using so that we may avoid it?
OK, I back to delete[] on no condition. lol
That's the whole point of setting pointers to NULL after deletion is to avoid dangling pointers. We cannot delete an invalid pointer but we can delete a valid NULL pointer. It's easy to work this into your design as well.
Last edited on
Topic archived. No new replies allowed.