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

  • Home
  • SEARCH
  • 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 546845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:54:35+00:00 2026-05-13T10:54:35+00:00

I have a problem with boost shared_memory_object and mapped_region. I want to write a

  • 0

I have a problem with boost shared_memory_object and mapped_region. I want to write a set of objects (structures) in the memory object. If the structure contains just a char, everything is ok; if I just add an int to the structure, then if I put too many objects (let’s say 70, so much less than the limit of the block) I get a segmentation fault when writing.

So far I have just seen examples where simple chars are written to the shared memory, but I have not read anything about the kind of objects that can be used. I am wondering if I have to make the conversion between my objects and a byte stream, or if such a function already exists. Or if I am just doing something wrong in my code. The commented lines are the ones that give me a segfault when decommented…

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <iostream>
#include <unistd.h>

struct Record {
    char c;
    int i;
//  float f;
//  double d;
//  char cs[32];
//  boost::interprocess::string is;
//  std::vector<int> v;

    Record() {}
    Record(int _k) { Init(_k); }

    void Init(int _k = 0) {
        c = _k + 65;
        i = _k;
//      f = _k + _k/100.0;
//      d = _k + _k/1000.0;
//      is = "interprocess string";
//      for(int j = 0; j < _k; ++j) v.push_back(j);
    }
};

int main(int argc, char *argv[])
{
   using namespace boost::interprocess;
   using std::cerr;
   using std::endl;

   int nObjects = 0;
   size_t blockSize = 1024;

   static std::string sharedObjName("MySharedMemory");      // why static?

   const int writer = 1, reader = 2, error = -1;
   int operation = error;

   if(argc >= 2) {
      if(argv[1][0] == 'w') operation = writer;
      if(argv[1][0] == 'r') operation = reader;
   }
   if(argc == 1) operation = writer;

   if(operation == writer)      // Writer process
   {
      cerr << "Number of objects to write = ";
      std::cin >> nObjects;

      // Remove shared memory on construction and destruction
      struct shm_remove {
         shm_remove() { shared_memory_object::remove(sharedObjName.c_str()); }
         ~shm_remove(){ shared_memory_object::remove(sharedObjName.c_str()); }
      } remover;

      shared_memory_object shm(create_only, sharedObjName.c_str(), read_write);

      shm.truncate(blockSize);

      mapped_region region(shm, read_write);

      offset_t shmSize;
      shm.get_size(shmSize);

      // Produce and write data
      Record *pData0 = static_cast<Record*>(region.get_address());
      Record *pData  = pData0;

      for(int i = 0; i < nObjects; ++i) {
         if(pData0 + blockSize - pData < signed(sizeof(Record))) {
            cerr << "Error: memory block full!" << endl;
            break;
         }
         pData->Init(i);
         pData += sizeof(Record);
      }

      //Launch child process
      pid_t pId = fork();

      if(pId == 0)
      {
         std::string s(argv[0]); s += " r";

         if(std::system(s.c_str()) != 0) {
            cerr << "Error launching reader process." << endl;
            exit(1);
         }
         exit(0);
      }
      else if(pId > 0)
      {
         sleep(2);
         cerr << "Writer has finished!" << endl;
      }
      else  // pId < 0
         exit(-1);
   }
   else if(operation == reader)         // Reader process
   {
      shared_memory_object shm (open_only, sharedObjName.c_str(), read_only);

      mapped_region region(shm, read_only);

      Record *pData = static_cast<Record*>(region.get_address());

      for(int i = 0; i < nObjects; ++i)  {
         // Print pData...
         pData += sizeof(Record);
      }
   }
   else
      exit(1);

   return 0;
}

Thank you for any hint!

MacOS X 10.6.2 –
gcc 4.2 –
Boost 1.41.0

  • 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-05-13T10:54:35+00:00Added an answer on May 13, 2026 at 10:54 am
    pData += sizeof(Record);
    

    That line is the problem. Pointer arithmetic means changes are in “units” of the underlying pointer type, in this case Record. So if you want to increment to the next record, you should do pData++, rather than pData += sizeof(Record), which will increase the pointer by 64 bytes (assuming sizeof(Record) is 8 – 8*8 = 64).

    You’ve got a similar pointer arithmetic error in the size check:

    pData0 + blockSize - pData < signed(sizeof(Record))
    

    You probably want something like:

    blockSize/sizeof(Record)-(pData-pData0) <= 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to store a boost::posix_time::ptime object in a Windows shared memory map for
I'm new in using boost and have a problem. I need shared_mutex function in
I have a problem getting boost::multi_index_container work with random-access and with orderd_unique at the
My application problem is the following - I have a large structure foo. Because
do you have any info about the subject? any problem with boost::shared_mutex in particular
i have a strange problem with QList and the boost:shared_ptr. I'm afraid i am
I have some code which waits for write operation on Shared Memory. If no
I have problem with boost::regex, this solution works only for one result in each
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with my query on C, I’m using the oci8 driver. This

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.