Solved

STL objects in shared memory

Posted on 1997-10-17
5
500 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:dhm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1171655
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.
0
 
LVL 3

Author Comment

by:dhm
ID: 1171656
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!)
0
 
LVL 5

Expert Comment

by:yonat
ID: 1171657
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.
0
 
LVL 3

Author Comment

by:dhm
ID: 1171658
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.)
0
 
LVL 5

Accepted Solution

by:
yonat earned 200 total points
ID: 1171659
Good luck!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question