Boost logo

Boost :

From: Gennaro Prota (gennaro_prota_at_[hidden])
Date: 2003-05-14 11:34:48

On Wed, 14 May 2003 12:39:09 +0200, Markus Werle
<numerical.simulation_at_[hidden]> wrote:

>In one of Herb Sutters articles I saw that
>after deleting a pointer (a pimpl) he assigns 0 afterwards
>which seems to me like a good idea.
>(see e.g. or
>Maybe there is a good reason (efficiency?)
>why checked_delete omits this extra step.
>Please explain.
>template<class T> inline void checked_delete(T * x)
> typedef char type_must_be_complete[sizeof(T)];
> delete x;
> // why not ?
> x = 0;

This is mostly appropriate for alt.comp.lang.learn.c-c++. But let's
take it briefly, to avoid noise here: first of all, you can't nullify
the pointer if all you have is a copy of it. Secondly, it can be
nullified with something like:

 #include <cstddef>

 template <typename T>
 void delete_and_null(T*& p) {
  delete p;
  p = NULL;

but then you can't pass an rvalue:

 int* f();
 delete_and_null( f() ); // can't do

Thirdly, nullifying the pointer is generally considered a way to
_hide_ bugs, rather than eliminating them. I don't want to be dogmatic
here, but I've never encountered the necessity to double delete


Boost list run by bdawes at, gregod at, cpdaniel at, john at