2

GIVEN that you have a fixed area of memory already allocated that you would like to use, what C or C++ libraries will allow you to store a dynamic structure (e.g. a hash) in that memory?

i.e. the hash library must not contain any calls to malloc or new, but must take a parameter that tells it the location and size of the memory it is permitted to use.

(bonus if the library uses offsets rather than pointers internally, in case the shared memory is mapped to different address spaces in each process that uses it)

OJW
  • 4,514
  • 6
  • 40
  • 48
  • Could precise your question with some code ? I don't see how, once the memory allocated, you could not store something in it. – MickaelFM May 12 '09 at 10:52
  • MickTaiwan: #include class my_shared_memory { public: std::vector V; } mem; mem.V.insert(mem.V.begin(), 3); printf("Value %d is at %p, not within allocated memory at %p\n", mem.V[0], &mem.V[0], &mem); – OJW May 13 '09 at 10:47

2 Answers2

7

You can write your own custom allocators for STL containers.

Dr.Dobb's: What Are Allocators Good For?

SO: Compelling examples of custom C++ STL allocators?

Community
  • 1
  • 1
alex vasi
  • 5,304
  • 28
  • 31
2

It's trivial to adapt a simple linear probing hash table to use a block of memory - just set its table(s) to point at the allocated memory when you create it, and don't implement anything to allocate more memory to let the table grow.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
  • I have one of those (as you say: quite trivial to create, especially if you don't need to delete items) but using a proper library might give better performance – OJW May 12 '09 at 14:34