Is it safe to delete[] of NULL?

Jun 7, 2014 at 7:54pm
1
2
int *p = NULL;
delete[] p;
Jun 7, 2014 at 8:01pm
Nope, I've hard crashes caused by this.

use:
if (p) delete[] p;
Jun 7, 2014 at 8:05pm
OK. Thanks.
Jun 7, 2014 at 8:11pm
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?
Jun 7, 2014 at 8:27pm
OK, I back to delete[] on no condition. lol
Jun 7, 2014 at 8:45pm
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 Jun 7, 2014 at 8:46pm
Topic archived. No new replies allowed.