Sign in to follow this  
Infinite_Daremo

C++ : Writing your own memory manager

Recommended Posts

Ive been looking into writing my own memory manager for a project at university but can't seem to find a suitable tutorial for one. Im looking to overwrite the new operator so i can add dynamic objects to a block of memory ive allocated. Anyone got a good article with some source code to get me started?

Share this post


Link to post
Share on other sites
Thanks for the articles guys.

I found this: http://www.codeproject.com/KB/cpp/MemoryPool.aspx

But i think what Servant of the Lord posted is the direction i should be going, if thats the case i best be prepared for a headache :). Either way im on the track to somewhere. Thanks again.

Share this post


Link to post
Share on other sites
An easier solution may be to consider creating an object pool and reusing your objects. The fastest call to new() is the one you don't make.

Also, reusing objects instead of creating new ones can help prevent memory fragmentation issues. It's probably also less likely to cause weird, hard-to-track bugs.

Share this post


Link to post
Share on other sites
Quote:
Also, reusing objects instead of creating new ones can help prevent memory fragmentation issues. It's probably also less likely to cause weird, hard-to-track bugs.
Just to offer a different point of view, I'll mention that sometimes reusing objects can cause weird, hard-to-track bugs.

Object-oriented C++ makes (or should make) heavy use of the RAII idiom, and reuse of existing objects is more or less counter to this. (Reuse of SC++L stream objects, for example, can often lead to unexpected program behavior.)

Personally, I recommend keeping RAII and instead making use of pooled memory allocation to accomplish the same ends (that is, faster creation and destruction of objects, and less fragmentation). One fairly straightforward way to accomplish this is to use the Boost Pool library.

Share this post


Link to post
Share on other sites
Quote:
Original post by kyoryu
True, but if you're using RAII, you're not creating objects on the heap with new(), you're creating them on the stack :)
I believe you are incorrect. RAII isn't just about automatic destruction of stack-based objects; an object of a type that exhibits RAII behavior (such as boost::shared_ptr or std::vector) will exhibit that behavior regardless of how its lifetime is managed or whether it is stack- or heap-allocated.

Share this post


Link to post
Share on other sites
Quote:
Original post by Infinite_Daremo
Ive been looking into writing my own memory manager for a project at university


Is the memory manager, in fact, a stated part of the project requirements? If not, why do you feel it will be necessary?

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by Infinite_Daremo
Ive been looking into writing my own memory manager for a project at university


Is the memory manager, in fact, a stated part of the project requirements? If not, why do you feel it will be necessary?


Yes its an advanced section. Either implement a memory manager, message system or an in built profiler.

Share this post


Link to post
Share on other sites
Hey again guys. Ive implemented a memory manager which works great for classes of fixed size. Problem now is with the use of std vectors, adding to it obviously increases the memory size and my manager doesnt know about it. Ive been told about allocators for the vector but the few articles ive looked at are way over my head, anyone know of another possible way to overcome this problem or a dumbed down guide to it?

Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by Infinite_Daremo
Ive been told about allocators for the vector but the few articles ive looked at are way over my head, anyone know of another possible way to overcome this problem or a dumbed down guide to it?


Set the capacity of vector on creation, then don't allow it to grow. Make sure it's big enough to hold maximum number of elements.

Otherwise, use list or similar with fixed-size allocator.

These would be the "dumbed" down versions.

Share this post


Link to post
Share on other sites

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