Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

STL objects in shared memory

Posted on 1997-10-17
5
Medium Priority
?
501 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 800 total points
ID: 1171659
Good luck!
0

Featured Post

Technology Partners: 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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

704 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