Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!wuarchive!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c++ Subject: Re: How to get the functionality of realloc() in C++ ? Message-ID: <733@taumet.com> Date: 17 May 91 15:42:09 GMT References: <1991May13.191807.29295@csl.dl.nec.com> <728@taumet.com> Organization: Taumetric Corporation, San Diego Lines: 35 roger@zuken.co.jp (Roger Meunier) writes: >In article <728@taumet.com> steve@taumet.com (Stephen Clamage) writes: > > WARNINGS: Do not use malloc/realloc for objects with > > constructors. Do not 'delete' something allocated with > > malloc/realloc. Do not 'free' something allocated with 'new'. >How is strdup() implemented in a C++ environment? Does it allocate >using 'new" or malloc()? The warnings were intended as rules for happy living. If you follow them, you are less likely to get into trouble. As to strdup(), it is not an standard ANSI C function, and as yet there is no standard C++ library. Not all systems have a strdup(). If it exists, it is most likely to be from the C library, and hence written in C. In that case, it would be likely to use malloc(), but who knows? It might use its own private memory scheme, like grabbing a chunk of memory with sbrk() and handing out pieces. You have a few choices: 1. Be a wimp. Don't try to recover the memory allocated to these strings. 2. Be safe. Write your own strdup -- it is a very tiny function. Use 'new' or 'malloc' as the fancy strikes you. 3. Play the odds. It is unlikely that strdup uses 'new', so just use 'free' to recover storage. 4. Throw caution to the winds, and use 'delete'. It is likely that 'new' and 'delete' just use the system 'malloc' and 'free' anyway. 5. Hack! Disassemble the library strdup to see what it calls, then write your code accordingly. Of course, you have to repeat this process for each library or system change. -- Steve Clamage, TauMetric Corp, steve@taumet.com