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<char, boost::interprocess::managed_shared_memory::segment_manager> CharAllocator;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, CharAllocator> my_string;

managed_shm.construct<my_string>("DataToShare")(data_to_share.c_str(), managed_shm.get_segment_manager());

Consumer Process:

boost::interprocess::managed_shared_memory managed_shm(boost::interprocess::open_read_only, "SomeName");
typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> CharAllocator;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, CharAllocator> my_string;

std::pair<my_string *, size_t > data = managed_shm.find<my_string>("DataToShare");

cout << "Data Size=" << data.first->length() << endl;

The problem is when I try to print the length of the DataToShare in the Consumer Process it is wrong, so what is the problem in my code, is it in the Producer Part since I am doing data_to_share.c_str()which is a std::string type instead of of my_string type?

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?