简体   繁体   中英

boost::interprocess — std::string vs. std::vector

I used boost::interprocess::managed_(windows_)shared_memory::construct to construct an interprocess vector holding an own class, which has a member variable of type std::string and another of type std::vector, so:

class myclass
{
    public:
        myclass()
        {

        }

        std::string _mystring;
        std::vector < int > _myintvector;
};

template < class _type >
struct typedefs
{
    typedef boost::interprocess::managed_windows_shared_memory     _memory;
    typedef _memory::segment_manager                               _manager;
    typedef boost::interprocess::allocator < _type, _manager >     _allocator;
    typedef boost::interprocess::vector < _type, _allocator >      _vector;
};

typedef typedefs < myclass > tdmyclass;

int main ()
{
    using namespace boost::interprocess;

    managed_windows_shared_memory mem ( open_or_create, "mysharedmemory", 65536 );
    tdmyclass::_vector * vec = mem.construct < tdmyclass::_vector > ( "mysharedvector" ) ( mem.get_segment_manager() );
    myclass mytemp;

    mytemp._mystring = "something";
    mytemp._myintvector.push_back ( 100 );
    mytemp._myintvector.push_back ( 200 );

    vec->push_back ( mytemp );

    /* waiting for the memory to be read is not what this is about, 
    so just imagine the programm stops here until everything we want to do is done */
}

I just did this for testing, I expected neither std::string nor std::vector to be working, yet, if I read it in from another process, std::string actually works, it contains the string I assigned. That really surprised me. The std::vector on the other side only partially works, the value returned by size() is correct, but the programm crashes if I want to access an iterator or by using operator[].

So, my question is, why is it that way ? I mean, I never actually read the STL implementations of the SDK of Visual Studio, but isn't std::string just an std::vector with extra functions suited for strings ? Don't they both use std::allocator - which would mean, that BOTH std::string and std::vector wouldn't work in shared memory ?

Googling for this doesn't really result into anything but boost::interprocess::vector, and thats not what I searched. So I hope someone can give me some details about whats going on ^^

PS: If I did a typo in the above code, please pardon me, I just wrote it right now in this pages editor and im kinda way too used to autocomplete of my IDE ^^

std::string works because your standard library implementation uses small-string-optimization (SSO). That means that the value of the string "something" is copied into the string object itself, without any dynamic memory allocations. Hence you can read it from the other process. For longer strings (try 30 characters) it won't work.

std::vector doesn't work because it's not allowed to use SSO by standard. It allocates the memory in the address space of the first process, but this memory is not accessible by the other process. The .size() works because it's stored inside the vector itself, as a member.

PS there are many differences between string and vector . The most important one, and the least mentioned, is that string is not a container from standard's point of view .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM