Jump to content
  • Advertisement
Sign in to follow this  
deadimp

C++ : A "realloc"-like function for memory allocated by "new"?

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

Bascially what the title says. I was browsing around C++ reference (in cstdlib) and I found realloc. Sadly enough, this is the first time I've really seen the function. Is there any such function for any memory allocated by the "new" operator?

Share this post


Link to post
Share on other sites
Advertisement
If you think you want such a thing, you really, really should first look at the standard library containers, in particular std::vector.

Share this post


Link to post
Share on other sites
Do you know if the STL vector class uses 'malloc'? If so, how does it call the constructor? If not, then what method does it use to reallocate?

Thanks for the replies.

Share this post


Link to post
Share on other sites
It uses operator new to allocate raw memory and copies each element into the buffer. Then it destroys the old objects and calls operator delete on the old memory.

[Edited by - RDragon1 on April 1, 2006 11:54:33 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by deadimp
Do you know if the STL vector class uses 'malloc'? If so, how does it call the constructor? If not, then what method does it use to reallocate?

Thanks for the replies.

Placement new

Basically, there are two types of new. One (the normal one) allocates memory using whatever method the runtime library chooses. The other (placement new) takes a pointer to some location in memory, and calls the constructor for an object is is to exist there. Classes like std::vector will often allocate a dynamic array of bytes, probably char. It will then use placement new to go through the array and construct all the elements.

Later, it will go through the array and explicitly call the destructor for each object. It then calls delete to deallocate the array of chars like normal.

Share this post


Link to post
Share on other sites
std::vector does no do allocation/deallocation and/or construction/destruction directly itself, it's std::vector's allocator type that does those things and as with all standard library containers it has a default allocator type std::allocator.

std::allocator does not use malloc/free it use explicit invocations of operator new/delete for allocation/deallocation and placement new todo in-place construction and explicitly invokes destructors for destruction. No it doesn't allocator arrays of type char since it's pointless & unnecessary when an explicit invocation of operator new/delete allocates/deallocates chunks of uninitialized memory.

Thats not to say you can't have a custom allocator type that use malloc/free instead with placement new you can, it's just that std::allocator doesn't.

Share this post


Link to post
Share on other sites
So placement new is somewhat like a pseudo-workaround for not being able to explicitly call the constructor?
Also, what kind of overhead is involved when using placement new (how would calling it once for 100 objects be different than calling it 100 times [for different arguments in constructr])?
Plus, are there any dangers when allocating buffers when you have a polymorphic class (the main concern is where the data for the vtable and such is stored)?
And (after reading some C++ FAQ Lite), in what cases would you need alignment for your class? Just when using arrays (not pointers to)?
And plus, (yes, many many questions) you will be able to use any memory allocated by malloc() for the raw memory, right (making completely sure)?

Would it be better if I created a lighter version of std::vector (removing any functions that I don't use) so that the object code is smaller? Or will that be basically useless?

Thanks.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by deadimp
Is there any such function for any memory allocated by the "new" operator?


As noted, the C++ equivalent to realloc is called vector::resize().

Quote:

Do you know if the STL vector class uses 'malloc'? If so, how does it call the constructor? If not, then what method does it use to reallocate?


I like to think of it as secret compiler voodoo. One of those "don't ask, don't tell" things.

Quote:

Plus, are there any dangers when allocating buffers when you have a polymorphic class (the main concern is where the data for the vtable and such is stored)?
And (after reading some C++ FAQ Lite), in what cases would you need alignment for your class? Just when using arrays (not pointers to)?


No, pointers to can still cause problems if what they point to isn't align properly.

Quote:

And plus, (yes, many many questions) you will be able to use any memory allocated by malloc() for the raw memory, right (making completely sure)?


Memory from malloc() is guaranteed to be properly aligned for anything you throw at it.

Quote:

Would it be better if I created a lighter version of std::vector (removing any functions that I don't use) so that the object code is smaller?


Most of std::vector will get inlined if you're in release mode. So, it comes down to one of two things:

1) You don't do as good a job as the authors of std::vector and you end up, after several (if not more) man hours of work, with a neutered version of std::vector that is probably slower and with more bugs.

2) You do as good a job as the authors of std::vector and you end up, after several (if not more) man hours of work, with a neutered version of std::vector that probably has more bugs.

Quote:

Or will that be basically useless?


Basically, yes.

Share this post


Link to post
Share on other sites
Forgive me if this is really dense, but how can malloc call a constructor when it has no information about the type of memory it is allocating? Same question for free and destructors? Perhaps I've misread the last post.

[EDIT] Sorry - ignore that stupid question.

[Edited by - EasilyConfused on April 4, 2006 3:52:21 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!