home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!hal.com!decwrl!csus.edu!netcomsv!mork!parag
- From: parag@netcom.com (Parag Patel)
- Newsgroups: comp.lang.c++
- Subject: Re: strdup() Was: Re: Renew?
- Message-ID: <87mmpnc.parag@netcom.com>
- Date: 28 Jul 92 21:43:30 GMT
- References: <23315@alice.att.com> <1992Jul25.151227.8156@hemlock.cray.com> <1992Jul27.173054.396@taumet.com> <1992Jul27.201834.13654@Warren.MENTORG.COM>
- Organization: Netcom - Online Communication Services (408 241-9760 guest)
- Lines: 21
-
- 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.
-
- My partner and I always create and use a "strfree(const char*)" which is
- just a simple inline function or macro that uses either malloc() or new
- depending on how strdup() is coded. We also allocate strings using
- "strnew(int length)" (also a macro or inline) which merely allocates
- space for a string of a given length plus one for the null byte.
-
- Then it's an easy matter to switch from new/delete to free/malloc
- without affecting any of our code. The code is a bit more readable too,
- and strnew() also eliminates the most common off-by-one error.
-
-
- -- Parag Patel <parag@Netcom.COM>
-