boost::interprocess——std::string 与 std::vector

发布于 2024-11-16 04:56:05 字数 1871 浏览 1 评论 0原文

我使用 boost::interprocess::managed_(windows_)shared_memory::construct 来构造一个持有自己的类的进程间向量,该向量有一个 std::string 类型的成员变量和另一个 std::vector 类型的成员变量,所以:

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 */
}

我只是这样做是为了测试,我预计 std::string 和 std::vector 都不起作用,但是,如果我从另一个进程读入它,std::string 实际上起作用,它包含我分配的字符串。这真的让我很惊讶。 另一侧的 std::vector 仅部分工作, size() 返回的值是正确的,但如果我想访问迭代器或使用运算符[],程序就会崩溃。

所以,我的问题是,为什么会这样?我的意思是,我从未真正阅读过 Visual Studio SDK 的 STL 实现,但 std::string 不只是一个带有适合字符串的额外函数的 std::vector 吗?难道它们都使用 std::allocator - 这意味着 std::string 和 std::vector 都不能在共享内存中工作吗?

谷歌搜索并没有真正得到任何结果,除了 boost::interprocess::vector ,而这不是我搜索的内容。所以我希望有人能给我一些关于发生了什么的细节^^

PS:如果我在上面的代码中犯了一个打字错误,请原谅我,我刚刚在这个页面编辑器中写了它,我有点太习惯自动完成了我的IDE ^^

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 ^^

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぇ气 2024-11-23 04:56:05

std::string 之所以有效,是因为您的标准库实现使用小字符串优化 (SSO)。这意味着字符串 "something" 的值被复制到字符串对象本身,而不进行任何动态内存分配。因此您可以从其他进程中读取它。对于较长的字符串(尝试 30 个字符),它将不起作用。

std::vector 不起作用,因为标准不允许它使用 SSO。它在第一个进程的地址空间中分配内存,但其他进程无法访问该内存。 .size() 之所以有效,是因为它作为成员存储在向量本身的内部。

PS stringvector 之间有很多区别。最重要也是最少提及的是 string从标准的角度来看,不是容器

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.

P.S. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文