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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!cs.utexas.edu!zaphod.mps.ohio-state.edu!sdd.hp.com!usc!wupost!darwin.sura.net!uvaarpa!murdoch!virginia.edu!gs4t
  3. From: gs4t@virginia.edu (Gnanasekaran Swaminathan)
  4. Subject: Re: strdup() Was: Re: Renew?
  5. Message-ID: <1992Jul30.161536.29107@murdoch.acc.Virginia.EDU>
  6. Sender: usenet@murdoch.acc.Virginia.EDU
  7. Reply-To: gs4t@virginia.edu (Gnanasekaran Swaminathan)
  8. Organization: University of Virginia
  9. References: <1992Jul27.201834.13654@Warren.MENTORG.COM> <1992Jul28.163356.3019@taumet.com> <1992Jul29.155252.7632@cbnewsk.cb.att.com>
  10. Date: Thu, 30 Jul 1992 16:15:36 GMT
  11. Lines: 21
  12.  
  13.  
  14. hansen@pegasus.att.com (Tony L. Hansen) writes:
  15. >    #include <string.h>
  16. >    char *nstrdup(const char *s)
  17. >    {
  18. >    if (!s) s = "";
  19. >    char *ret = new char[strlen(s) + 1];
  20. >    if (ret) strcpy(ret, s);
  21. >    return ret;
  22. >    }
  23.  
  24. Elegant answer but has a little problem.
  25.  
  26. char* ptr = nstrdup(0); // ptr = ""; not allocated by new
  27. delete [] ptr;        // error
  28.  
  29. Therefore, I suggest the following change.
  30.  
  31. if (!s) return 0;
  32.  
  33. -Sekar
  34.