home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!cbnewsm!cbnewsk!pegasus!hansen
- From: hansen@pegasus.att.com (Tony L. Hansen)
- Subject: Re: strdup() Was: Re: Renew?
- Organization: AT&T
- Date: Wed, 29 Jul 1992 15:52:52 GMT
- Message-ID: <1992Jul29.155252.7632@cbnewsk.cb.att.com>
- Summary: nstrdup()
- References: <1992Jul27.173054.396@taumet.com> <1992Jul27.201834.13654@Warren.MENTORG.COM> <1992Jul28.163356.3019@taumet.com>
- Sender: hansen@cbnewsk.cb.att.com (tony.l.hansen)
- Lines: 62
-
- < From: steve@taumet.com (Steve Clamage)
- <
- < adk@Warren.MENTORG.COM (Ajay Kamdar) writes:
- <
- << Given that new and malloc are not necessarily related, deleteing memory
- << returned from strdup() [STRINGS(3)] could be a recipe for disaster since
- << the returned memory is malloced and not newed. It is onerous to keep
- << track of which memory is newed, and which is malloced. C++ code written
- << using strdup() might work with one compiler but not with another if care
- << is not exercised in freeing the returned memory.
- <<
- << Is there any move towards providing C++ified versions of functions like
- << strdup() as part of some standard library? Right now the only solution I
- << know is to implement my own version of such functions.
- <
- < Good analysis.
- <
- < The strdup() function is not part of Standard C, and is thus not being
- < contemplated for Standard C++.
- <
- < Since strdup is so common, I don't know why it was not made part of
- < Standard C. Does anyone know whether it was dicussed by X3J11 and if so,
- < why it was rejected?
-
- If I remember correctly, it was in there for a while and then got removed in
- a later draft. A number of implementations added the function to their
- libaries during the time that the function was part of the draft standard.
- Since it was such a useful function, it was usually left in. I don't
- remember why it was removed from the draft standard.
-
- < Since strdup is a very tiny function, your best bet is to write one
- < yourself for use with C++, using new rather than malloc if you wish.
- <
- < You will run into problems with header files and library organization if
- < you call it strdup, so probably you should pick a different name.
-
- This is very important. If I make a call to strdup() I EXPECT it to have
- been implemented using malloc() and I will use free() on the string later
- on.
-
- At least one library provider (Zinc) provides a version of strdup() which
- uses new, but they named it something like nstrdup() or ui_nstrdup().
- Nowadays, when writing C++ code that needs the functionality of strdup(),
- I'll use my own version which uses new that I usually call nstrdup().
-
- By the way, at least one person has posted a poor version of strdup() which
- didn't check the return value of new. Here's my contribution. As you can
- see, it is a very straightforward function:
-
- #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;
- }
-
- Tony Hansen
- hansen@pegasus.att.com, tony@attmail.com
- att!pegasus!hansen, attmail!tony
-
-