Advertisement

realloc in C++

Started by June 01, 2001 07:01 PM
18 comments, last by benjamin bunny 23 years, 8 months ago
Is there a new/delete equivilent of realloc()? www.elf-stone.com

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

Afaik, no there isn''t. You can use realloc on the same pointer though. Or you can just delete it and then use new with it.

Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/ (If my website isn''t down, it''s a miracle!)
Advertisement
delete and then new doesn''t do the same thing as realloc, and reallocing memory allocated with new causes an exception. There must be some way of doing it, surely?



www.elf-stone.com

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

Really? That''s weird (about the realloc and new). I had assumed it would still work, but I guess I was wrong (new and free work together, I guess I just assumed too much). And you''re correct about it not doing the same thing (I wasn''t thinking ), you''d have to throw a memcpy into there too, and that would be a pain. I don''t know, I haven''t got any more good answers...

Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/ (If my website isn''t down, it''s a miracle!)
quote:
Original post by Null and Void

Really? That''s weird (about the realloc and new). I had assumed it would still work, but I guess I was wrong (new and free work together, I guess I just assumed too much). And you''re correct about it not doing the same thing (I wasn''t thinking ), you''d have to throw a memcpy into there too, and that would be a pain. I don''t know, I haven''t got any more good answers...

Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/ (If my website isn''t down, it''s a miracle!)


the STL provide something much more flexible than realloc. Vectors. There will resize themselves at will without you even knowing it or having to worry about the details.

There is one reason to use realloc over anything else: when you need the guarantee that the memory block will still remain at the same address. I can''t think of any reasons this would be essential, but it''s something new/delete and vector don''t guarantee.
Advertisement
realloc doesnt garuntee that either.


for example, if you realloc something to a size larger than what can fit in the empty space after the original block, it will have no choice but to move the entire block to another place.



And, i think the reason that realloc doesnt work with new/delete is because it doesnt take into account for the class headers.

===============================================
It''s either ether or the other,
I press my window to the glass,
the glass turns to gas...
I''m going upstairs now, to turn my mind off...
to turn my mind off...
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.
I believe the reason it''s not supported is because there''s now an implicit constructor and destructor call associated with memory allocation and deallocation. I also believe this is why there''s the collision with using malloc and free alongside new and delete. If you step into new, there''s a malloc deep down in there. Youc an overload new to try reallocing, though I don''t attempt such things myself.
You should never use C memory management functions together with new or delete! new and delete take the constructor and destructor of classes into account, while the C memory management functions don''t.

If you need realloc for some sort of array implementation, you can better use STL''s vector (or another container that serves your need).

HTH
new and delete can also be easily overridden (and often are for debugging purposes). Just because you accidentally stumbled on a case where new is apparently using malloc to get it''s memory doesn''t mean that it will still be doing so tomorrow.

-Mike
-Mike

This topic is closed to new replies.

Advertisement