home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11746 < prev    next >
Encoding:
Internet Message Format  |  1992-07-29  |  1.5 KB

  1. Path: sparky!uunet!sun-barr!ames!agate!forney.berkeley.edu!jbuck
  2. From: jbuck@forney.berkeley.edu (Joe Buck)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: strdup() Was: Re: Renew?
  5. Date: 29 Jul 1992 19:54:15 GMT
  6. Organization: U. C. Berkeley
  7. Lines: 25
  8. Message-ID: <156t17INNq72@agate.berkeley.edu>
  9. References: <1992Jul27.201834.13654@Warren.MENTORG.COM> <1992Jul28.163356.3019@taumet.com> <1992Jul29.155252.7632@cbnewsk.cb.att.com>
  10. NNTP-Posting-Host: forney.berkeley.edu
  11.  
  12. In article <1992Jul29.155252.7632@cbnewsk.cb.att.com> hansen@pegasus.att.com (Tony L. Hansen) writes:
  13. >By the way, at least one person has posted a poor version of strdup() which
  14. >didn't check the return value of new. Here's my contribution. As you can
  15. >see, it is a very straightforward function:
  16. >[ nstrdup that checks value of new ]
  17.  
  18. Sorry, Tony, I disagree.  This is the whole reason that exceptions were
  19. added to the language, so we wouldn't have to do this.  In the case of
  20. new failures, there is already a mechanism present: the "new handler".
  21.  
  22. If nstrdup checks for new returning 0, we then have to check for nstrdup
  23. returning 0, and so on.  Your nstrdup has just passed the problem on to
  24. someone else.
  25.  
  26. The proper approach is to use set_new_handler establish a handler to catch
  27. new returning 0, and have it either free up some memory or exit the
  28. application cleanly.  Those in memory-poor environments (IBM-PCs and
  29. such) may disagree.
  30.  
  31. Given this philosophy, my version was just fine, thank you.
  32.  
  33.  
  34.  
  35. --
  36. Joe Buck    jbuck@ohm.berkeley.edu
  37.