Link to home
Start Free TrialLog in
Avatar of dhm
dhm

asked on

STL objects in shared memory

I'd like to use STL containers & etc in a multi-threaded program, but I need to have any memory allocated by the container, and by any objects *in* the container, allocated from a shared memory arena.  Furthermore, I have to be able to do this (relatively) portably between NT and various unixen (I use M$VC++ on NT and g++/libg++ on all the Unix boxes).

So:

How do I force all new() and delete()s for container-related objects to be satisfied from a special pool?

Is there a common memory-manager template that can be given a chunk of memory to manage?  It'll have to handle alloc/free, of course, but probably also need to coalesce fragments, because its "heap" will be of a fixed size.
Avatar of nietod
nietod

There is a design facet of STL that I though was totally unnecessary, but I was wrong because that is exactly what you need.  

The STL containers use allocator object to allocate and dispose of memory.  The allocator is usually the last parameter to the container's template definition.  However, you probably never specified it because it defaults to a default allocator that uses operator new and operator delete.

I've never done this before, but I believe all you have to do is define a template class derived from the STL's default allocator.   you will need to redefine the procedures that allocate and dispose of memory and maybe a few others.  The specify this class as the last parameter to the STL container template.

This should work in standard c++ on all platforms.
Avatar of dhm

ASKER

You're right about allocators (I've been rooting around in MSVC++'s STL headers), but the problem is g++ -- the latest version can't compile SGI's STL library (the one with allocators in its templates), and HP's library is too old to have them.  I guess the question is really more about the portability aspect ("how-to-do-portably") than the simple "how-to-do."

Any leads on the memory manager?  I'll accept an answer for either part (but would prefer both!)
Allocators are the way to go.
You can get a portable version of the STL from
http://www.ipmce.su/~fbp/stl/
It works for g++ and many other compilers.
Avatar of dhm

ASKER

Cool...I found egcs and got it to work last night, but this gives me an escape route if egcs has other problems with my code.  Thanks!  (You have to submit this as an answer so I can grade it & get you the points.)
ASKER CERTIFIED SOLUTION
Avatar of yonat
yonat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial