cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
Is it safe to delete[] of NULL?
Is it safe to delete[] of NULL?
Jun 7, 2014 at 7:54pm UTC
Arrandale
(8)
1
2
int
*p = NULL;
delete
[] p;
Jun 7, 2014 at 8:01pm UTC
Stewbond
(2827)
Nope, I've hard crashes caused by this.
use:
if
(p)
delete
[] p;
Jun 7, 2014 at 8:05pm UTC
Arrandale
(8)
OK. Thanks.
Jun 7, 2014 at 8:11pm UTC
cire
(8284)
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 UTC
Arrandale
(8)
OK, I back to delete[] on no condition. lol
Jun 7, 2014 at 8:45pm UTC
NoXzema
(699)
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 UTC
Topic archived. No new replies allowed.