Click to See Complete Forum and Search --> : Using Realloc with the result of New?


Jason Mccullough
July 8th, 1999, 11:57 AM
I've just come upon something that I had never thought of before: can realloc() be used on a ptr that was assigned by the new opperator? And if so, will the opperator delete function correctly?

Roger C
July 8th, 1999, 12:15 PM
I can't quote a definate answer for you, but I would strongly caution against doing this even if your compiler will allow it. Write a realloc function that uses new to allocate a larger buffer, memcpy, and delete the original.

Roger C. Scudder Jr.
Annance Consulting
Wayne, Pennsylvania, US

Paul McKenzie
July 8th, 1999, 12:23 PM
The answer is "don't do it". realloc() and new() are different things that are not to be mixed together. The new() calls constructors, while realloc has no idea of objects or constructors.

You can't mix a call to new() with free(), or malloc() with delete(). The same thing goes with new() with realloc()/calloc().

Regards,

Paul McKenzie