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

Started by
11 comments, last by GameDev.net 18 years ago
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?
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Advertisement
unfortunately, no. you'll just have to create a function that does that for you.

Beginner in Game Development?  Read here. And read here.

 

If you think you want such a thing, you really, really should first look at the standard library containers, in particular std::vector.
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.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
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]
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.

"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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.
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.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
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.
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]

This topic is closed to new replies.

Advertisement