Sign in to follow this  

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

This topic is 4276 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
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
The constructors are called by the C++ compiler, because he has the knowlegde about types. The C++ compiler only asks for enough memory to hold the object, you don't need to know the type inside new if you're just serving this memory.

Share this post


Link to post
Share on other sites
Quote:
Original post by deadimp
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)?

Only resort to the dark sticky corners of the language if you're really sure you need to and you know what you're doing.

Placement new is really intended for systems programming where you have hardware registers at specific memory locations, or for use in general container class libraries where templated code needs to get "inside" the C++ object model. Any other use is really just abuse.

If you need more fine-tuned storage control than the standard runtime gives you, what you need is class-specific new/delete operators.

You are unlikely to find a valid reason to replace language-supplied constructs like the new operator with hand-rolled stuff gleaned from C.
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? Or will that be basically useless?

One of the joys of templated classes is that if a member function is not referenced, it's not instantiated. So, by rewriting a carefully designed and widely tested piece of software all you've achieved is possibly slower, bigger, buggier code. You can, of course, chalk it up to a learning experience. Learning is good. Just don't assume that, as a beginner, you're going to write better stuff than the folks with decades of experience and years of peer reviews and production-level testing have.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by EasilyConfused
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.


There are no stupid questions. You figured it out, but in case there's anyone else (and if there's one, there's probably more), placement new is essentially a way to call a constructor on an arbitrary piece of memory. The syntax of placement new requires the type, so that's how it knows which constructor. You have to call the destructor manually (this is the only time you should manually call a destructor!) before you clean up the memory (free() if you malloc()'d it, letting an array pass out of scope, etc.).

Be careful, if you got the chunk of memory using new and then use placement new to construct the object, you still have to manually call the destructor and you should still use delete to free the memory.

Share this post


Link to post
Share on other sites

This topic is 4276 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this