Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!uunet!cme!durer.cme.nist.gov!warsaw From: warsaw@nlm.nih.gov (Barry A. Warsaw) Newsgroups: comp.lang.c++ Subject: Curious about operator delete setting ptrs to zero... Message-ID: Date: 7 Jan 91 16:36:07 GMT Sender: news@cme.nist.gov Reply-To: warsaw@nlm.nih.gov Organization: Century Computing, Inc. Lines: 45 Having been bitten lately with multiple destruction of freestore allocated objects, I'm curious as to why operator delete does not, by default, set the pointer whose object is being deleted to zero? I'm sure there must be some good reasons why this is not done by default in C++, and I can perhaps(?) come up with some scenarios where this might be a Bad Thing, but in general why can't delete perform this very useful function, and allow overloading of delete to bypass when this would cause problems due to a particular class's implementation. Aside from the reason that the language definition would have to change to facilitate this (would it? see below). Seems to me it would be a useful thing since deleting a null pointer should be harmless but deleting an already freed block can have disasterous effects. Just to quickly illustrate with a totally contrived example: #include class Foo { public: Foo( int val ) { data = val; } int value() { return( data ); } private: int data; }; main() { Foo* foo = new Foo( 10 ); cout << "foo = " << foo << ", foo's value is " << foo->value() << endl; delete foo; cout << "foo = " << foo << endl; // foo = 0; // necessary since delete doesn't set foo=0; delete foo; // if delete set foo=0, this would be harmless; }; So enlighten me as to why the current functionality. Also, can you provide a hint as to how one might go about overloading operator delete or writing a destructor to give a class the ability to set the pointer to zero? As I see it, it would currently be difficult since `this' doesn't get passed to the destructor by reference and operator delete's first argument is a void*. Seems to me it would have to be a void*& to allow setting to zero. Comments? -Barry