Jump to content
  • Advertisement

Archived

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

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

This topic is 5377 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

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

Share this post


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

Share this post


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

Share this post


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

Share this post


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


delete []p;


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

Thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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)

Share this post


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


delete []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]

Share this post


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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!