new and realloc

Started by
3 comments, last by postalbruhman99 22 years, 8 months ago
is there a c++ operator to reallocate memory? if not, how are some ways i can do that?
"And considering that my free time is severly fragmented (and my free time degragmenter is buggy)....."~SiCrane, patron of gamedev.net
Advertisement
Allocate a new block of memory, copy the data across with memcpy or memmove, then delete the old block. Of course, this means that the addresses of the old and new blocks will be different. But needing to rely on the address being the same is not usually a very good design decision anyway.
Hi,

Try using realloc.

Sorry, I spoke before thinking

I've tested whether realloc actually works with new/delete, and it seems to do so for fundamental types (tested under the borland compiler). I haven't tested it with classes; that would involve having to manually call the constructors and destructors, which I'm not too interested in trying to do.

The easiest solution is, as you'll see:

#include <vector.h>

...

vector MyIntArray(5); // int array with initial size 5.
MyIntArray[50] = 8; // array grows in size as needed.

Edited by - Beer Hunter on August 13, 2001 4:19:50 AM

This topic is closed to new replies.

Advertisement