Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8729733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:53:08+00:00 2026-06-13T08:53:08+00:00

I have a simple requirement that might be tough to solve. I did find

  • 0

I have a simple requirement that might be tough to solve. I did find some leads like this or this but I can’t seem to readilly use them. The former doesn’t even translate into buildable code for me. I am not experienced with Boost to just write this on my own but it seems to me this might be a common requirement.

I have also come across Interprocess STL Map but I have not yet been able to assemble it into working code.

I am thinking boost::interprocess is the way to go here, unless I want to create some shared memory map from scratch.

I am not concerned with portability. I need a solution that will work with MS compiler, specifically the one that comes with VS 2010.

This poster seems to want to more or less what I am trying to do, except I need to map a GUID to an arbitrary length binary buffer (but an int to string is equally good as a starting point). Unfortunately, I cannot compile the code cleanly to even begin with experiments.

Also I have two concerns: A) is it possible to automatically (or at least predictably) grow/shrink the shared memory to accommodate allocation needs and B) assuming one process creates the map, how can another process “attach” to it?

I don’t mind if a solution requires multiple shared “segments” in order to satisfy allocation needs. It doesn’t necessarily have to be a single monolithic shared chunk of memory.

Any help is highly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:53:09+00:00Added an answer on June 13, 2026 at 8:53 am

    Here’s recent example which i had written to learn the usage of maps in Shared memory. It compiles so probably, you can experiment with it to suit your requirement.

    The code for a server process which creates Shared memory and puts map into it:-

    #include <boost/interprocess/managed_shared_memory.hpp>
    #include <boost/interprocess/containers/map.hpp>
    #include <boost/interprocess/allocators/allocator.hpp>
    #include <functional>
    #include <utility>
    
    int main ()
    {
        using namespace boost::interprocess;
    
        // remove earlier existing SHM
        shared_memory_object::remove("SharedMemoryName");
    
        // create new 
        managed_shared_memory segment(create_only,"SharedMemoryName",65536);
    
        //Note that map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
        //so the allocator must allocate that pair.
        typedef int    KeyType;
        typedef float  MappedType;
        typedef std::pair<const int, float> ValueType;
    
        //allocator of for the map.
        typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;
    
        //third parameter argument is the ordering function is used to compare the keys.
        typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MySHMMap;
    
        //Initialize the shared memory STL-compatible allocator
        ShmemAllocator alloc_inst (segment.get_segment_manager());
    
        // offset ptr within SHM for map 
        offset_ptr<MySHMMap> m_pmap = segment.construct<MySHMMap>("MySHMMapName")(std::less<int>(), alloc_inst);
    
        //Insert data in the map
        for(int i = 0; i < 10; ++i)
        {
                m_pmap->insert(std::pair<const int, float>(i, (float)i));
        }
    
        return 0;
    }
    

    The code for a client process which attaches to the memory and accesses map’s data.

    #include <boost/interprocess/managed_shared_memory.hpp>
    #include <boost/interprocess/containers/map.hpp>
    #include <boost/interprocess/allocators/allocator.hpp>
    #include <functional>
    #include <utility>
    #include <iostream>
    
    int main ()
    {
        using namespace boost::interprocess;
    
        try
        {
    
                managed_shared_memory segment(open_or_create, "SharedMemoryName",65536);
    
                //Again the map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>, so the allocator must allocate that pair.
                typedef int    KeyType;
                typedef float  MappedType;
                typedef std::pair<const int, float> ValueType;
    
                //Assign allocator 
                typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;
    
                //The map
                typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MySHMMap;
    
                //Initialize the shared memory STL-compatible allocator
                ShmemAllocator alloc_inst (segment.get_segment_manager());
                                                                                            
                //access the map in SHM through the offset ptr                                                         
                MySHMMap :: iterator iter;
                offset_ptr<MySHMMap> m_pmap = segment.find<MySHMMap>("MySHMMapName").first;
    
                iter=m_pmap->begin();
                for(; iter!=m_pmap->end();iter++)
                {
                       std::cout<<"\n "<<iter->first<<" "<<iter->second;
                }
        }catch(std::exception &e)            
        {
                std::cout<<" error  " << e.what() <<std::endl;
                shared_memory_object::remove("SharedMemoryName");
        }
        return 0;
    }
                                                                                                                   
    

    The Code for the Client Process explains how using "names" and an offset pointer, other processes can attach and access the Map contents created in SHM by the server process.
    But, allocating size (here its ‘65536’) while creating a new shared memory segment, i am not sure whether the size can be shrinked, though probably you can create more chunks of shared memory for expanding the SHM…

    Hope it helped…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement which sounds like kind of simple but hard to implement
I have a requirement to create a simple windows forms application that allows an
I have to implement a strange requirement that goes as follows: Users can hover
This is very simple, but I'm being rather stupid about it. I have a
We have a simple requirement to use https for certain specific pages in a
I have a simple requirement, i need a map of type . however i
I have a simple requirement as mentioned below: A ListView or any control displays
I am using django 1.4 and Python 2.7. I just have a simple requirement
I have an ASP ListView, and have a very simple requirement to display numbers
I have a few HTML files that I'd like to include via tags in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.