Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!udel!haven.umd.edu!mimsy!prometheus!media!hqda-ai!fstc-chville.army.mil!adm!lhc!lhc!warsaw From: warsaw@nlm.nih.gov (Barry A. Warsaw) Newsgroups: comp.lang.c++ Subject: Re: Object pointers, and destructors Message-ID: Date: 5 Jun 91 16:08:01 GMT References: <19944@sdcc6.ucsd.edu> <2289@godzilla.tcs.com> Sender: usenet@nlm.nih.gov (usenet news poster) Reply-To: warsaw@nlm.nih.gov Distribution: comp Organization: Century Computing, Inc. Lines: 46 In-Reply-To: robert@kohlrabi.tcs.com's message of 31 May 91 22:59:58 GMT >>>>> "Robert" == Robert Blumen writes: Robert> You could do this yourself by writing your own ::operator Robert> delete and linking it into your code. Are you sure? Doesn't ::operator delete *require* an argument type of void* and forbid void*&, which would be necessary if setting the pointer to zero after deleting would have any effect outside of the operator? I don't have the ARM around, but I know my 2.0 cfront barfs on: void ::operator delete( void*& ptr ) { ... }; CC foo.cc: "foo.cc", line 7: error: operator delete()'s 1st argument must be a void* But perhaps I'm missing something? Robert> I can't say just why it doesn't do this, since not doing Robert> so means that the post-delete pointer value is certainly Robert> invalid. When I asked this question 6-8 months ago, people correctly pointed out that just setting the pointer to zero won't solve all your problems, for example in cases where two pointers are referencing the same object, you delete the first pointer, it gets set to zero, but then you accidently dereference the second pointer. How are you going to set all pointers referencing some chunk of memory to zero? Not in any built-in way, that for sure. And it *does* cost enough performance-wise to warrant not being built into the language. Robert> It is consistent with the C++ philosophy "it doesn't work Robert> this way by default but you can do it yourself" Hmm. I for one would've liked to see ::operator delete take a void*& so you *could* do this if you wanted (which, right now, you can't). Still after listening to what more experienced programmers were saying, and getting some C++ time under my belt, I think you really need to take a much more careful approach when throwing pointers to objects around. Using ::operator delete could lull one into a false sense of security :-). -Barry "When in doubt, cout."