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

  1. Path: sparky!uunet!usc!sdd.hp.com!swrinde!cs.utexas.edu!torn!cunews!nrcnet0!bnrgate!bcrka451!bcrki65!sjm
  2. From: sjm@bcrki65.bnr.ca (Stuart MacMartin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: strdup() Was: Re: Renew?
  5. Message-ID: <1992Jul31.141149.11647@bcrka451.bnr.ca>
  6. Date: 31 Jul 92 14:11:49 GMT
  7. References: <9221219.25266@mulga.cs.mu.OZ.AU> <1992Jul27.173054.396@taumet.com> <1992Jul27.201834.13654@Warren.MENTORG.COM> <1992Jul28.163356.3019@taumet.com> <1992Jul29.155252.7632@cbnewsk.cb.att.com>
  8. Sender: 5E00 Corkstown News Server
  9. Reply-To: sjm@bcrki65.bnr.ca (Stuart MacMartin)
  10. Organization: Bell-Northern Research, Ottawa, Canada
  11. Lines: 41
  12.  
  13. In article <9221219.25266@mulga.cs.mu.OZ.AU>, fjh@munta.cs.mu.OZ.AU
  14. (Fergus James HENDERSON) writes:
  15. > hansen@pegasus.att.com (Tony L. Hansen) writes:
  16. > >By the way, at least one person has posted a poor version of strdup() which
  17. > >didn't check the return value of new. Here's my contribution. As you can
  18. > >see, it is a very straightforward function:
  19. > >
  20. > >    #include <string.h>
  21. > >    char *nstrdup(const char *s)
  22. > >    {
  23. > >    if (!s) s = "";
  24. >     ^^^^^^^^^^^^^^^
  25. > >    char *ret = new char[strlen(s) + 1];
  26. > >    if (ret) strcpy(ret, s);
  27. > >    return ret;
  28. > >    }
  29. > If you ask me, the check for ``if (!s) s = "";'' is a really bad idea.
  30. > If the program passes you a NULL pointer, then you do NOT want to silently
  31. > correct it, you want to let the programmer know that an error has occured.
  32.  
  33. This is a matter of whether or not you consider this to be an error.
  34. In C, it was annoying that realloc did not treat NULL as a request for
  35. malloc.  It is personal preference whether this should be allowed in realloc 
  36. or handled in a higher-level macro.  Similarly, if free(s) did nothing if
  37. s was NULL, it would have saved a lot of bugs due to people forgetting to do
  38. if (s) free(s).
  39.  
  40. This case is slightly different, because there is more than one obvious
  41. way to handle 0 legally:  return 0 or return a pointer to "".  Personally,
  42. I don't consider the gain made by treating 0 input as an error to be worth
  43. the pain.  Anyone who wants to write robust code will use your less efficient
  44. form.  And anyway, the strcpy(ret,s) instead of memcpy(ret,s,len) is a far
  45. more expensive coding luxury.  (Set len = strlen(s) + 1 before the new).
  46.  
  47. Stuart
  48.  
  49. : Stuart MacMartin                                    email: sjm@bnr.ca      :
  50. : Bell-Northern Research                              phone: (613) 763-5625  :
  51. : PO Box 3511, Stn C, Ottawa, K1Y-4H7, CANADA    Standard disclaimers apply. :
  52.