Thread: STL in Shared Memory

  1. #1
    Registered User
    Join Date
    Jul 2010

    Question STL in Shared Memory

    Hello everyone,

    First of all, sorry for my bad english..

    I'm trying to put a hash_map into a shared memory segment. I know all the issues about shared memory containing STL containers (google is full of it), but my hashmap would be preallocated and fixed size, I just need to share it so all other apps can access data from it, no changes would be made.

    Can anyone give me some hints, or a simple code part on how to do that !?

    Best regards,

  2. #2
    Registered User
    Join Date
    Jun 2005
    It would be preferable if you used a container that is actually specified in the C++ standard (or, if you want to push the edge a bit, that is likely to be specified in an upcoming release of the standard). hash_map is not standard.

    In any event, if you look at the specification of hash_map (with those compilers/libraries that supply it) or any of the standard containers, you will note one of the template parameters is an allocator type.

    All you need to do is create a custom allocator type that obtains memory from your shared memory area, and use an instance of that allocator type instead of the default allocator when creating your container.

    You may - depending on the workings of the shared memory - also need to use some synchronisation mechanism, such as a mutex, to prevent (say) instances where one application modifies the memory while another is attempting to read or modify it.
    Last edited by grumpy; 07-15-2010 at 04:07 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy HWND into shared memory buffer
    By Opariti in forum Windows Programming
    Replies: 2
    Last Post: 12-26-2009, 01:08 PM
  2. Memory Fragmentation with Dynamic FIFO Queue
    By fguy817817 in forum Linux Programming
    Replies: 17
    Last Post: 10-31-2009, 04:17 AM
  3. Partly shared memory
    By DrSnuggles in forum C++ Programming
    Replies: 13
    Last Post: 01-21-2009, 03:35 AM
  4. shared memory not getting freed
    By Elkvis in forum Linux Programming
    Replies: 19
    Last Post: 02-29-2008, 04:48 PM
  5. shared memory can not read value over 255
    By jbsloan in forum C Programming
    Replies: 4
    Last Post: 04-03-2005, 11:56 AM

Tags for this Thread