Path: utzoo!attcan!uunet!jarthur!uci-ics!gateway From: schmidt@crimee.ics.uci.edu (Doug Schmidt) Newsgroups: comp.lang.c++ Subject: Re: Renew? Keywords: memory allocation Message-ID: <26601546.25889@paris.ics.uci.edu> Date: 27 May 90 17:58:30 GMT References: <1990May25.013535.16930@cbnewsl.att.com> Reply-To: schmidt@crimee.ics.uci.edu (Doug Schmidt) Organization: University of California, Irvine - Dept of ICS Lines: 44 In-reply-to: bill@cbnewsl.att.com (william.clark) In article <1990May25.013535.16930@cbnewsl.att.com>, bill@cbnewsl (william.clark) writes: >C has malloc() and C++ has new >C has remalloc() could C++ have "renew"? > >It would save me some time shifting class contents around as the objects >accrue data. (I know I could simple use malloc() and realloc() and might >even save some time but I'd rather stick to new). The GNU libg++ library has an extension along these lines in the file: ---------------------------------------- // provide a C++ interface to vector-resize via realloc static inline void *operator new(size_t size, void *ptr, size_t new_len) { return realloc(ptr, new_len * size); } ---------------------------------------- You use this extension as follows: ---------------------------------------- void resize (size_t elem_size, void *old_ptr, int new_size) { // The following line uses overloaded operator new to // emulate realloc. return new {old_ptr, new_size * elem_size} char; } ---------------------------------------- Note that g++ uses { } as the delimiters for the placement syntax, cfront 2.0 would use ( ), e.g.: return new (old_ptr, new_size * elem_size) char; Note that since operator new is declared inline there should be no additional function call overhead for using this `re'new abstraction. Doug -- The official language of San Marcos is Swedish. All boys | schmidt@ics.uci.edu under the age of sixteen years old are now sixteen years | office (714) 856-4043 old. Underwear must be changed every half hour. It will +---------------------- be worn on the outside, so we can check. -- `Bananas' by Woody Allen