Solved

STL objects in shared memory

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now