realloc what is the equivalent in C++ with new?

Started by
15 comments, last by MARS_999 20 years, 7 months ago
I am using new to allocate memory for my textures. Instead of using new and calling delete for each texture how can I allocate memory once and like with c, I think its realloc(), reallocate memory for a new size so I don''t fragment my memory? Thanks
Advertisement
Here is an example and the definition of how to use realloc.

Hope this helps.

- BlueDev
[/quote]
Not really due to you can''t mix and match malloc or C dynamic allocation methods with C++ new and delete dynamic allocation. I was wondering how C++ does it if it has some keyword or C++ function like realloc? Thanks
quote:Original post by MARS_999
Not really due to you can''t mix and match malloc or C dynamic allocation methods with C++ new and delete dynamic allocation. I was wondering how C++ does it if it has some keyword or C++ function like realloc? Thanks

No, it does not.
There is no "renew" or other equivalent to realloc in C++. This really isn''t a big deal, though. Just allocate your new buffer, use memcpy to copy the contents of the old buffer to the new one, and then delete the old buffer. This is functionally equivalent to what realloc does, anyways.
You could always define your own ''new''s that can perform a realloc operation behind the scenes. That''s the only C++ friendly way I can think of... and having thought of it, I think I''m off to code myself up one.
I was told doing new and delete and new and delete will fragment memory??

int *p = new int[100];//do stuff with p[]delete []p;int *p = new int[1000];//do stuff againdelete []p;


now you get the idea? now do that for e.g. 20x or 100x
will that fragment memory???

Thanks
The equivalent of using malloc/realloc in C is to use std::vector in C++.

Anyway, I wouldn''t worry about fragmentation. Realloc can''t do anything more than what the library can do with a delete and a successive new. In fact, realloc needs to preserve the block contents, which may be a bit of a disadvantage (and certainly causes an unnecessary copy if you want to re-write the block).

Don''t optimize until you know that you have to. Fragmented heaps are REALLY hard to come by on modern computers (although it does happen in really big projects. Like, REALLY big)
quote:Original post by MARS_999
I was told doing new and delete and new and delete will fragment memory??

int *p = new int[100];//do stuff with p[]delete []p;int *p = new int[1000];//do stuff againdelete []p;


now you get the idea? now do that for e.g. 20x or 100x
will that fragment memory???

Thanks



As the AP said, you really don't need to worry about this. The big thing is make sure you don't create any leaks.

What you showed also doesn't really pertain to the OP since we are talking about expanding an array not repeatedly newing and deleting.

In any event, "expanding" you array size does result in creation of a new larger buffer and copying the contents of the old one over, no matter what.

If you find yourself frequently enlarging your buffer, you should probably use a different strategy anyways, such as using vectors or allocating a buffer that you believe to be large enough and then use that. If you use the latter solution, you will need to check your bounds. Assumptions can be dangerous.


[edited by - jeeky on August 31, 2003 12:07:10 AM]
quote:Original post by Anonymous Poster
The equivalent of using malloc/realloc in C is to use std::vector in C++.


Not quite, but almost. std::vector does give you risizable arrays and overloaded [], but you can''t treat the vector as a contiguous memory block.

For example, you can''t use a std::vector in OpenGL vertex array calls. This is the biggest problem with std::vector that I see in graphics programming. If anyone knows a way around it, let me know.

This topic is closed to new replies.

Advertisement