[boost] Re: checked_delete without assignment to 0 - why?

2003-05-14 Thread Alexander Nasonov
Markus Werle wrote:

 Hi!
 
 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. http://www.gotw.ca/gotw/028.htm or http://tinyurl.com/bq8o)
 
 Maybe there is a good reason (efficiency?)
 why checked_delete omits this extra step.
 Please explain.
 
 templateclass T inline void checked_delete(T * x)
 {
   typedef char type_must_be_complete[sizeof(T)];
   delete x;
   // why not ?
   x = 0;
 }
 

checked_delete only checks, it doesn't improve you code :)
Seriously, the question can be redirected to C++98 level: why delete 
operator doesn't assign deleted pointer to 0? I'm sure it has already been 
discussed. You can search for the topic in comp.std.c++ and 
comp.lang.c++.moderated.
-- 
Alexander Nasonov
Remove minus and all between minus and at from my e-mail for timely response


___
Unsubscribe  other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


[boost] Re: checked_delete without assignment to 0 - why?

2003-05-14 Thread Gennaro Prota
On Wed, 14 May 2003 12:39:09 +0200, Markus Werle
[EMAIL PROTECTED] wrote:

Hi!

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. http://www.gotw.ca/gotw/028.htm or http://tinyurl.com/bq8o)

Maybe there is a good reason (efficiency?) 
why checked_delete omits this extra step.
Please explain.

templateclass 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
anything.


Genny.

___
Unsubscribe  other changes: http://lists.boost.org/mailman/listinfo.cgi/boost