home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!cs.utexas.edu!zaphod.mps.ohio-state.edu!sdd.hp.com!usc!wupost!darwin.sura.net!uvaarpa!murdoch!virginia.edu!gs4t
- From: gs4t@virginia.edu (Gnanasekaran Swaminathan)
- Subject: Re: strdup() Was: Re: Renew?
- Message-ID: <1992Jul30.161536.29107@murdoch.acc.Virginia.EDU>
- Sender: usenet@murdoch.acc.Virginia.EDU
- Reply-To: gs4t@virginia.edu (Gnanasekaran Swaminathan)
- Organization: University of Virginia
- References: <1992Jul27.201834.13654@Warren.MENTORG.COM> <1992Jul28.163356.3019@taumet.com> <1992Jul29.155252.7632@cbnewsk.cb.att.com>
- Date: Thu, 30 Jul 1992 16:15:36 GMT
- Lines: 21
-
-
- hansen@pegasus.att.com (Tony L. Hansen) writes:
- > #include <string.h>
- > char *nstrdup(const char *s)
- > {
- > if (!s) s = "";
- > char *ret = new char[strlen(s) + 1];
- > if (ret) strcpy(ret, s);
- > return ret;
- > }
-
- Elegant answer but has a little problem.
-
- char* ptr = nstrdup(0); // ptr = ""; not allocated by new
- delete [] ptr; // error
-
- Therefore, I suggest the following change.
-
- if (!s) return 0;
-
- -Sekar
-