ba124@citycs.UUCP (K.A.Streater) (05/18/89)
In C++ there are the free storage allocators new and delete, but what about an equivalent of realloc. Have I missed it or does it not exist ? Stroustrup in his example on page 182 of C++ Programming language seems to indicate that the only way to realloc a block of storage is to create a new one, and copy all the elements before deleting the old block. i.e. in 'C': #include <stdlib.h> /* ANSI header */ int *address = (int *)malloc(size); address = (int *)realloc(address, size*2); becomes in C++: int* address = new int[size]; int* new_addr = new int[size*2]; for (int i=0; i<size; i++) new_addr[i] = address[i]; delete address; address = new_addr; Any ideas for a better solution than this ?
jima@hplsla.HP.COM (Jim Adcock) (05/22/89)
Good question. I suspect the answer, in part, is going to lie in the implementation of a particular C++ compiler's implementation of new and delete -- which I haven't yet seen documented on any compilers. Why not just continue to use malloc/realloc? If you just need a chunk of memory you should be able to continue to use these as always. If you actually need this feature for object creation and destruction, then presumably you need the capability rumored for future release in C++ compilers -- the ability to take over allocation/deallocation for a particular class by overloading new/delete.
ba124@citycs.UUCP (K.A.Streater) (05/25/89)
In article <6590130@hplsla.HP.COM>, jima@hplsla.HP.COM (Jim Adcock) writes: > Good question. I suspect the answer, in part, is going > to lie in the implementation of a particular C++ compiler's > implementation of new and delete -- which I haven't yet > seen documented on any compilers. Why not just continue > to use malloc/realloc? If you just need a chunk of memory > you should be able to continue to use these as always. > > If you actually need this feature for object creation and > destruction, then presumably you need the capability > rumored for future release in C++ compilers -- the ability > to take over allocation/deallocation for a particular class > by overloading new/delete. The idea I first put forward was to get away from malloc/realloc. The Zortech C++ uses malloc & free to implement new and delete so why not add a new operator e.g. extend which will take an object which has been new'ed and extend it. i.e. class array; // previously implemented array* fred = new array[10]; ... fred = extend fred array[100]; which would copy the old contents of fred and add another 90 objects. Does this sound too far away from the C++ ideals ???? Finally, I thought you could overload new/delete anyway. -- K.A.Streater, JANET: ba124@uk.ac.city.cs UUCP: ba124@citycs.UUCP ARPA: ba124@cs.city.ac.uk "There was a point to this story, but it has temporarily escaped the Chronicler's mind.", Hitch-Hiker's Guide Part IV.