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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!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: Fri, 31 Jul 1992 14:39:32 GMT
  7. Message-ID: <1992Jul31.143932.13345@cbnewsk.cb.att.com>
  8. Summary: look again
  9. Keywords: strdup, null pointers
  10. References: <1992Jul28.163356.3019@taumet.com> <1992Jul29.155252.7632@cbnewsk.cb.att.com> <1992Jul30.161536.29107@murdoch.acc.Virginia.EDU>
  11. Sender: hansen@cbnewsk.cb.att.com (tony.l.hansen)
  12. Lines: 59
  13.  
  14. < From: gs4t@virginia.edu (Gnanasekaran Swaminathan)
  15. <
  16. < hansen@pegasus.att.com (Tony L. Hansen) writes:
  17. <<    #include <string.h>
  18. <<    char *nstrdup(const char *s)
  19. <<    {
  20. <<    if (!s) s = "";
  21. <<    char *ret = new char[strlen(s) + 1];
  22. <<    if (ret) strcpy(ret, s);
  23. <<    return ret;
  24. <<    }
  25. <
  26. < Elegant answer but has a little problem.
  27. <
  28. < char* ptr = nstrdup(0); // ptr = ""; not allocated by new
  29. < delete [] ptr;        // error
  30. <
  31. < Therefore, I suggest the following change.
  32. <
  33. < if (!s) return 0;
  34.  
  35. Look again.
  36.  
  37. It's not the value of "" which is returned, but a new'd copy of "" which is
  38. returned. If the pointer passed in is a null pointer, then that pointer is
  39. replaced with a value of "", which is then copied into the new'd space. In
  40. other words, it's always the value which came from operator new which is
  41. returned, and never the value of "".
  42.  
  43. Here's a version of nstrdup() which is heavily commented:
  44.  
  45. /*
  46.     NAME
  47.     nstrdup - duplicate string into new'd heap space
  48.  
  49.     SYNOPSIS
  50.     char *nstrdup(const char *string)
  51.  
  52.     DESCRIPTION
  53.  
  54.     The nstrdup() function allocates storage space (with a call to
  55.     operator new) for a copy of string, and returns a pointer to the
  56.     storage space containing the copied string. The function returns 0
  57.     if operator new returned 0. If the string pointer is null, an empty
  58.     string is used in its place.
  59. */
  60.     
  61. #include <string.h>
  62. char *nstrdup(const char *s)
  63. {
  64.     if (!s) s = "";              // if null pointer, use empty string default
  65.     char *ret = new char[strlen(s) + 1];  // allocate space
  66.     if (ret) strcpy(ret, s);          // copy string
  67.     return ret;                  // return value returned by operator new
  68. }
  69.  
  70.                     Tony Hansen
  71.                 hansen@pegasus.att.com, tony@attmail.com
  72.                 att!pegasus!hansen, attmail!tony
  73.