Archived

This topic is now archived and is closed to further replies.

new and realloc

This topic is 5964 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites