home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11696 < prev    next >
Encoding:
Internet Message Format  |  1992-07-28  |  1.5 KB

  1. Path: sparky!uunet!olivea!hal.com!decwrl!csus.edu!netcomsv!mork!parag
  2. From: parag@netcom.com (Parag Patel)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: strdup() Was: Re: Renew?
  5. Message-ID: <87mmpnc.parag@netcom.com>
  6. Date: 28 Jul 92 21:43:30 GMT
  7. References: <23315@alice.att.com> <1992Jul25.151227.8156@hemlock.cray.com> <1992Jul27.173054.396@taumet.com> <1992Jul27.201834.13654@Warren.MENTORG.COM>
  8. Organization: Netcom - Online Communication Services  (408 241-9760 guest)
  9. Lines: 21
  10.  
  11. adk@Warren.MENTORG.COM (Ajay Kamdar) writes:
  12.  
  13. >Given that new and malloc are not necessarily related, deleteing memory
  14. >returned from strdup() [STRINGS(3)] could be a recipe for disaster since the
  15. >returned memory is malloced and not newed. It is onerous to keep track of
  16. >which memory is newed, and which is malloced. C++ code written using strdup()
  17. >might work with one compiler but not with another if care is not exercised
  18. >in freeing the returned memory.
  19.  
  20. My partner and I always create and use a "strfree(const char*)" which is
  21. just a simple inline function or macro that uses either malloc() or new
  22. depending on how strdup() is coded.  We also allocate strings using
  23. "strnew(int length)" (also a macro or inline) which merely allocates
  24. space for a string of a given length plus one for the null byte.
  25.  
  26. Then it's an easy matter to switch from new/delete to free/malloc
  27. without affecting any of our code.  The code is a bit more readable too,
  28. and strnew() also eliminates the most common off-by-one error.
  29.  
  30.  
  31.     -- Parag Patel <parag@Netcom.COM>
  32.