Solved

STL objects in shared memory

Posted on 1997-10-17
5
496 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

735 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