Memory manager/class allocation question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Memory manager/class allocation question

  1. #1
    Join Date
    Jan 2006
    Posts
    2

    Post 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.

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    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

  3. #3
    Join Date
    Jan 2006
    Posts
    2
    Danny, thanks for the response. I'll give this a go around and see.

Similar Threads

  1. Memory Question
    By Filbert in forum Talk to the Editors
    Replies: 1
    Last Post: 09-20-2005, 06:03 PM
  2. help memory
    By kathy in forum Java
    Replies: 0
    Last Post: 11-02-2001, 04:22 PM
  3. Interface leaks in C#?
    By Kevin Burton in forum .NET
    Replies: 12
    Last Post: 10-09-2000, 10:29 AM
  4. VB Arrays and memory allocation
    By Brian Leung in forum VB Classic
    Replies: 12
    Last Post: 06-20-2000, 03:06 PM
  5. VB Arrays and memory allocation
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center