[comp.lang.c++] Renew?

bill@cbnewsl.att.com (william.clark) (05/25/90)

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).

					-bill

schmidt@crimee.ics.uci.edu (Doug Schmidt) (05/28/90)

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
<new.h> 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