patterncppCritical
Is it safe to delete a NULL pointer?
Viewed 0 times
safedeletepointernull
Problem
Is it safe to delete a NULL pointer?
And is it a good coding style?
And is it a good coding style?
Solution
delete performs the check anyway, so checking it on your side adds overhead and looks uglier. A very good practice is setting the pointer to NULL after delete (helps avoiding double deletion and other similar memory corruption problems).I'd also love if
delete by default was setting the parameter to NULL like in #define my_delete(x) {delete x; x = NULL;}(I know about R and L values, but wouldn't it be nice?)
Code Snippets
#define my_delete(x) {delete x; x = NULL;}Context
Stack Overflow Q#4190703, score: 319
Revisions (0)
No revisions yet.