在 boost.interprocess 中从共享内存进行 memcpy 的问题

发布于 2024-08-23 06:56:23 字数 1794 浏览 10 评论 0原文

这让我沮丧得发疯。我只是想创建一个共享内存缓冲区类,该类在通过 Boost.Interprocess 创建的共享内存中使用,我可以在其中读取/存储数据。我编写了以下代码来测试功能

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
using namespace std;
using namespace boost::interprocess;

int main( int argc, char* argv[] ) {
    shared_memory_object::remove( "MyName" );
    // Create a shared memory object
    shared_memory_object shm ( create_only, "MyName", read_write );
    // Set size for the shared memory region
    shm.truncate(1000);
    // Map the whole shared memory in this process
    mapped_region region(shm, read_write);
    // Get pointer to the beginning of the mapped shared memory region
    int* start_ptr;
    start_ptr = static_cast<int*>(region.get_address());

    // Write data into the buffer
    int* write_ptr = start_ptr;
    for( int i= 0; i<10; i++ ) {
        cout << "Write data: " << i << endl;
        memcpy( write_ptr, &i, sizeof(int) );
        write_ptr++;
    }

    // Read data from the buffer
    int* read_ptr = start_ptr;
    int* data;
    for( int i= 0; i<10; i++ ) {
        memcpy( data, read_ptr, sizeof(int) );
        cout << "Read data: " << *data << endl;
        read_ptr++;
    }

    shared_memory_object::remove( "MyName" );
    return 0;
}

当我运行它时,它会正常写入数据,但在读取循环中的第一个 memcpy 上出现段错误。 gdb 说如下:

程序收到信号EXC_BAD_ACCESS,无法访问内存。 原因:KERN_INVALID_ADDRESS,地址:0x0000000000000000 __memcpy () 中的 0x00007fffffe007c5

(gdb) 其中

__memcpy () 中的 #0 0x00007fffffe007c5 #1 0x0000000100000e45 in main (argc=1, argv=0x7fff5fbff9d0) at try.cpp:36

功能非常简单,我不知道我错过了什么。任何帮助将不胜感激。

This is driving me wild with frustration. I am just trying to create a shared memory buffer class that uses in shared memory created through Boost.Interprocess where I can read/store data. I wrote the following to test the functionality

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
using namespace std;
using namespace boost::interprocess;

int main( int argc, char* argv[] ) {
    shared_memory_object::remove( "MyName" );
    // Create a shared memory object
    shared_memory_object shm ( create_only, "MyName", read_write );
    // Set size for the shared memory region
    shm.truncate(1000);
    // Map the whole shared memory in this process
    mapped_region region(shm, read_write);
    // Get pointer to the beginning of the mapped shared memory region
    int* start_ptr;
    start_ptr = static_cast<int*>(region.get_address());

    // Write data into the buffer
    int* write_ptr = start_ptr;
    for( int i= 0; i<10; i++ ) {
        cout << "Write data: " << i << endl;
        memcpy( write_ptr, &i, sizeof(int) );
        write_ptr++;
    }

    // Read data from the buffer
    int* read_ptr = start_ptr;
    int* data;
    for( int i= 0; i<10; i++ ) {
        memcpy( data, read_ptr, sizeof(int) );
        cout << "Read data: " << *data << endl;
        read_ptr++;
    }

    shared_memory_object::remove( "MyName" );
    return 0;
}

When I run this, it writes the data OK, but segfaults on the first memcpy in the read loop. gdb says the following:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
0x00007fffffe007c5 in __memcpy ()

(gdb) where

#0 0x00007fffffe007c5 in __memcpy ()
#1 0x0000000100000e45 in main (argc=1, argv=0x7fff5fbff9d0) at try.cpp:36

The functionality is so simple, I don't know what I'm missing. Any help will be much appreciated.

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

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

发布评论

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

评论(2

轻许诺言 2024-08-30 06:56:23

data 未设置为指向任何内容。 (确保在启用所有警告的情况下编译程序。)无论如何,它看起来都不应该是一个指针。

第二个循环也许应该是:

int* read_ptr = start_ptr;
int data;
for( int i= 0; i<10; i++ ) {
    memcpy( &data, read_ptr, sizeof(int) );
    cout << "Read data: " << data << endl;
    read_ptr++;
}

data isn't being set to point at anything. (Make sure the program is being compiled with all warnings enabled.) It looks like it shouldn't be a pointer anyway.

The second loop should perhaps be:

int* read_ptr = start_ptr;
int data;
for( int i= 0; i<10; i++ ) {
    memcpy( &data, read_ptr, sizeof(int) );
    cout << "Read data: " << data << endl;
    read_ptr++;
}
冷夜 2024-08-30 06:56:23

我无法在这里测试它,因为我没有可用的 boost 但我有猜测。在此 示例
shared_memory_object 对象首先使用 create_only 标志进行写入。

shared_memory_object shm (create_only, "MySharedMemory", read_write);

然后使用带有标志 open_only 的第二个 shared_memory_object 对象读取它:

shared_memory_object shm (open_only, "MySharedMemory", read_only);

看来您必须将 shared_memory_object 更改为正确的标志。

I can't test it here because I don't have boost available but I a have guess. In this example,
the shared_memory_object object is first used to write with a flag create_only.

shared_memory_object shm (create_only, "MySharedMemory", read_write);

Then it is read with a second shared_memory_object object with a flag open_only:

shared_memory_object shm (open_only, "MySharedMemory", read_only);

It seems you have to change your shared_memory_object to a proper flag.

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