views:

21

answers:

1

Is it possible to convert a boost::interprocess::string to an std::string or to a const char*? Something like c_str()...

E.g.:

boost::interprocess::string is = "Hello world";
const char* ps = is.c_str();    // something similar
printf("%s", ps);

I could even get a copy of the string in a non-shared memory block.

E.g.:

boost::interprocess::string is = "Hello world";
const char cs[100];
strcpy(cs, is.c_str());    // something similar
printf("%s", cs);

Thank you!

+1  A: 

boost::interprocess::string has a standard c_str() method. I found the following here:

//! <b>Returns</b>: Returns a pointer to a null-terminated array of characters 
//!   representing the string's contents. For any string s it is guaranteed 
//!   that the first s.size() characters in the array pointed to by s.c_str() 
//!   are equal to the character in s, and that s.c_str()[s.size()] is a null 
//!   character. Note, however, that it not necessarily the first null character. 
//!   Characters within a string are permitted to be null. 
const CharT* c_str() const 
{  return containers_detail::get_pointer(this->priv_addr()); }

(That's for the basic_string. string is a template instantiation in which the CharT template parameter is char.)

Also, the documentation here says

basic_string is the implementation of std::basic_string ready to be used in managed memory segments like shared memory. It's implemented using a vector-like contiguous storage, so it has fast c string conversion...

SCFrench
Thank you SCFrench. c_str() is what I tried at first, and I do not know why it didn't work. Now it is all right.
Pietro