-
Memory manager/class allocation question
Hi,
I'm thinking about implementing a c++ memory manager that would be based on pooling, or bucketing, objects of the same size. Thus, when a new request came in for some size_t chunk of memory, I'd try to allocate a chunk, of the same size, from the appropriate bucket.
My main concern with this approach is re-using memory as the footprint for a class that was not the original class when the memory was first allocated. In other words, will I end up with a corrupted object?
Ideally, a user would:
(1) call new <class>, as normal
(2) and the constructors would do the rest (that is, re-initialize all members).
My question is, would the compiler do anything wacky like store metadata particular to the original class such that, when the memory is re-used for a different class, memory corruption occurs?
And, if no memory corruption occurs -is this behavior guaranteed on future compilers/platforms?
Any help would be appreciated.
Thanks.
-
You have to distinguish between raw memory for which C++ store no special metadata (i.e., RTTI, exception stuff etc.) and objects, which are pieces of raw memory that have been initialized by a class's ctor. Your aim is obviously to preallocate a pool of the former type, and then recycle that memory, not reconstruct an object over the memory of an existing object( doing so would cause many nasty surprises such as a destructor of the original object being called on the newly constructed object etc.).
What you need is some form of placement new. This operaor allows you to construct an object on a pre-allocated memory address, under the assumption that address contains properly aligned memory with the appropriate size. You can find more info about placement new here:
http://www.devx.com/DevX/LegacyLink/9485
Gee, this was my first 10 Minute Solution way back in 1999 and it's still useful today!
Danny Kalev
-
Danny, thanks for the response. I'll give this a go around and see.
Similar Threads
-
By Filbert in forum Talk to the Editors
Replies: 1
Last Post: 09-20-2005, 06:03 PM
-
Replies: 0
Last Post: 11-02-2001, 04:22 PM
-
By Kevin Burton in forum .NET
Replies: 12
Last Post: 10-09-2000, 10:29 AM
-
By Brian Leung in forum VB Classic
Replies: 12
Last Post: 06-20-2000, 03:06 PM
-
By Brian Leung in forum VB Classic
Replies: 0
Last Post: 06-20-2000, 09:47 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks