From 8490461329193330879 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,176c9dc06a6729eb X-Google-Attributes: gidf78e5,public X-Google-ArrivalTime: 2002-04-08 10:11:04 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!colt.net!kibo.news.demon.net!news.demon.co.uk!demon!mail2news.demon.co.uk!not-for-mail From: Richard Damon Newsgroups: comp.std.c++ Subject: Re: why doesn't delete NULL the pointer? Date: Mon, 8 Apr 2002 17:10:33 GMT Organization: Beltronics Inc Approved: Fergus Henderson , moderator of comp.std.c++ Message-ID: References: <3C9DD892.94860335@acm.org> <3C9E5921.735FEB25@acm.org> <3CA063FF.3C2EE83A@acm.org> <3CA0FD94.A13F8343@acm.org> <3CA26183.B5822E43@acm.org> <3CADD3C8.EDA111C6@webmaster.com> Reply-To: richard_damon@iname.com X-Trace: mail2news.demon.co.uk 1018285839 mail2news:13105 mail2news mail2news.demon.co.uk X-Complaints-To: abuse@demon.net X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 93 Xref: archiver1.google.com comp.std.c++:10437 David Schwartz wrote: >Ken Alverson wrote: > >> I still feel my analogy is valid, but this really isn't going anywhere, so >> I'll drop the issue. >> >> Ken > > So what do you do if the value of the pointer is still needed?! > >Consider the following code: > >Mutex mutex_table[256]; > >void LockMutex(void *p) >{ > mutex_table[reinterpret_cast(p)%256].Lock(); >} > >void UnlockMutex(void *p) >{ > mutex_table[reinterpret_cast(p)%256].Unlock(); >} > >void ConditionalLockedDelete(Foo *p) >{ > LockMutex(p); > if(p->ShouldDelete()) delete p; > UnlockMutex(p); >} correct would be (need to change def of (Un)LockMutex { int hash = HashPointer(p); LockMutex(hash); if(p->ShouldDelete()) delete p; UnlockMutex(hash); } This doesn't reaccess the pointer after deleting, and hopefully will encourage you to use a better hash function. A large portion of your mutex_table is not used as the bottom bits of p are often the same to enforce alignment requirements, and if Foo is large some implementations may page align p and your table will only have one usable mutex. > > How would this work if 'delete' NULLed the pointer? Here's another >case: > >class Foo >{ > protected: > Foo *brother; > bool can_delete; > public: > static bool Detach(Foo *a) > { > if(can_delete) { delete a; return true; } > return false; > } > void BrotherNowGone(void) { brother=NULL; } > Foo *GetBrother() { return brother; } >} > >void DetachAndDeLink(Foo *a, Foo *b) >{ Foo *temp = a; > bool did_delete=a->Detach(); > if(did_delete && (b->GetBrother()==a)) b->BrotherNowGone(); use temp instead of a now. >} > > How would this work if 'delete' NULLed the pointer? > > DS > >--- >[ comp.std.c++ is moderated. To submit articles, try just posting with ] >[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] >[ --- Please see the FAQ before posting. --- ] >[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ] -- richard_damon@iname.com (Redirector to my current best Mailbox) rdamon@beltronicsInspection.com (Work Adddress) Richad_Damon@msn.com (Just for Fun) --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]