home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19771 < prev    next >
Encoding:
Text File  |  1993-01-23  |  1.8 KB  |  43 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Re: Dangling pointers & suggestions on future implementations of "delete"
  5. Message-ID: <1993Jan22.193322.20710@ucc.su.OZ.AU>
  6. Sender: news@ucc.su.OZ.AU
  7. Nntp-Posting-Host: extro.ucc.su.oz.au
  8. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  9. References: <Jan18.210305.21697@yuma.ACNS.ColoState.EDU> <1993Jan19.193048.15022@borland.com> <1993Jan20.213807.20740@cbnewsi.cb.att.com>
  10. Date: Fri, 22 Jan 1993 19:33:22 GMT
  11. Lines: 30
  12.  
  13. In article <1993Jan20.213807.20740@cbnewsi.cb.att.com> rhw@att.com (Robert Wentworth) writes:
  14. >In article <1993Jan19.193048.15022@borland.com> pete@borland.com (Pete Becker) writes:
  15. >>In article <Jan18.210305.21697@yuma.ACNS.ColoState.EDU> bb760597@longs.LANCE.ColoState.Edu (Blake Buhlig) writes:
  16. >>>I have been trying to find a way to overload the delete operator to set its
  17. >>>pointer argument to NULL.  This would help prevent dangling pointer 
  18. >>>dereferencing
  19. >
  20. >I agree: simply setting the deleted pointer to NULL or some illegal value
  21. >leaves a big hole because other pointers can still point to the same
  22. >now non-existent object.
  23.  
  24.     However, it is very useful to set a pointer to null after
  25. deleting it because then subsequent deletes will do no harm.
  26. Often, there is ONLY one pointer to each object.
  27. How about:
  28.  
  29.     template<class T> 
  30.     void destroy(T*& p){ delete p; p=0;}
  31.  
  32. Note: 'delete' cant set its argument to 0 because the argument
  33. mightn't be an lvalue: 
  34.  
  35.     delete p+1;
  36.  
  37.  
  38. -- 
  39. ;----------------------------------------------------------------------
  40.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  41.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  42. ;------ SCIENTIFIC AND ENGINEERING SOFTWARE ---ph:  2 799 8223 --------
  43.