STL in Shared Memory

This is a discussion on STL in Shared Memory within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, First of all, sorry for my bad english.. I'm trying to put a hash_map into a shared memory ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    1

    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,
    AlenDev

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,843
    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%.

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, 12: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, 02:35 AM
  4. shared memory not getting freed
    By Elkvis in forum Linux Programming
    Replies: 19
    Last Post: 02-29-2008, 03: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21