home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11723 < prev    next >
Encoding:
Text File  |  1992-07-29  |  3.0 KB  |  75 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!cbnewsm!cbnewsk!pegasus!hansen
  3. From: hansen@pegasus.att.com (Tony L. Hansen)
  4. Subject: Re: strdup() Was: Re: Renew?
  5. Organization: AT&T
  6. Date: Wed, 29 Jul 1992 15:52:52 GMT
  7. Message-ID: <1992Jul29.155252.7632@cbnewsk.cb.att.com>
  8. Summary: nstrdup()
  9. References: <1992Jul27.173054.396@taumet.com> <1992Jul27.201834.13654@Warren.MENTORG.COM> <1992Jul28.163356.3019@taumet.com>
  10. Sender: hansen@cbnewsk.cb.att.com (tony.l.hansen)
  11. Lines: 62
  12.  
  13. < From: steve@taumet.com (Steve Clamage)
  14. <
  15. < adk@Warren.MENTORG.COM (Ajay Kamdar) writes:
  16. <
  17. << Given that new and malloc are not necessarily related, deleteing memory
  18. << returned from strdup() [STRINGS(3)] could be a recipe for disaster since
  19. << the returned memory is malloced and not newed. It is onerous to keep
  20. << track of which memory is newed, and which is malloced. C++ code written
  21. << using strdup() might work with one compiler but not with another if care
  22. << is not exercised in freeing the returned memory.
  23. <<
  24. << Is there any move towards providing C++ified versions of functions like
  25. << strdup() as part of some standard library? Right now the only solution I
  26. << know is to implement my own version of such functions.
  27. <
  28. < Good analysis.
  29. <
  30. < The strdup() function is not part of Standard C, and is thus not being
  31. < contemplated for Standard C++.
  32. <
  33. < Since strdup is so common, I don't know why it was not made part of
  34. < Standard C.  Does anyone know whether it was dicussed by X3J11 and if so,
  35. < why it was rejected?
  36.  
  37. If I remember correctly, it was in there for a while and then got removed in
  38. a later draft. A number of implementations added the function to their
  39. libaries during the time that the function was part of the draft standard.
  40. Since it was such a useful function, it was usually left in. I don't
  41. remember why it was removed from the draft standard.
  42.  
  43. < Since strdup is a very tiny function, your best bet is to write one
  44. < yourself for use with C++, using new rather than malloc if you wish.
  45. <
  46. < You will run into problems with header files and library organization if
  47. < you call it strdup, so probably you should pick a different name.
  48.  
  49. This is very important. If I make a call to strdup() I EXPECT it to have
  50. been implemented using malloc() and I will use free() on the string later
  51. on.
  52.  
  53. At least one library provider (Zinc) provides a version of strdup() which
  54. uses new, but they named it something like nstrdup() or ui_nstrdup().
  55. Nowadays, when writing C++ code that needs the functionality of strdup(),
  56. I'll use my own version which uses new that I usually call nstrdup().
  57.  
  58. By the way, at least one person has posted a poor version of strdup() which
  59. didn't check the return value of new. Here's my contribution. As you can
  60. see, it is a very straightforward function:
  61.  
  62.     #include <string.h>
  63.     char *nstrdup(const char *s)
  64.     {
  65.     if (!s) s = "";
  66.     char *ret = new char[strlen(s) + 1];
  67.     if (ret) strcpy(ret, s);
  68.     return ret;
  69.     }
  70.  
  71.                     Tony Hansen
  72.                 hansen@pegasus.att.com, tony@attmail.com
  73.                 att!pegasus!hansen, attmail!tony
  74.  
  75.