Writing a dynamic string into a shared memory using Boost Library
I want to share a dynamic array of characters between two processes using
Boost Shared Memory. I use the following two pieces of code to do that:
*Producer Process:*
char *data;unsigned int share_length;unsigned int offset;
std::string data_to_share(data + offset, data + offset + share_length);
boost::interprocess::managed_shared_memory
managed_shm(boost::interprocess::create_only,
"SomeName",
1024*1024);typedef boost::interprocess::allocator
El 09/06/2014 1:59, Hany Assasa escribió:
*Possible Answer:*
I believe the answer to my question is, converting |std::string| into |my_string| type. so can anyone tell me how to do that?
my_string ms(std_string.c_str(), shm_allocator); or my_string ms(std_string.begin(), std_string.end(), shm_allocator); There area many possible ways using my_string constructor. You must always pass an allocator. However I don't think that's your problem. Do you have internal zero bytes in "char *data"? Just check if strlen(data) is equal to data_to_share.size(). If you have internal zeroes, use (data_to_share.begin(), data_to_share.end(), managed_shm.get_segment_manager()); to build the shared memory string. Best, Ion
participants (2)
-
Hany Assasa
-
Ion Gaztañaga