gpt4 book ai didi

c++ - 使用不同于 char 的类型 boost shared_memory_object 问题

转载 作者:搜寻专家 更新时间:2023-10-31 02:00:26 24 4
gpt4 key购买 nike

我对 boost shared_memory_object 和 mapped_region 有疑问。我想在内存对象中写一组对象(结构)。如果该结构只包含一个字符,则一切正常;如果我只是向结构中添加一个 int,那么如果我放入太多对象(比如 70,比 block 的限制少得多),我在写入时会出现段错误。

到目前为止,我只是看到了将简单字符写入共享内存的示例,但我还没有阅读任何关于可以使用的对象类型的内容。我想知道是否 必须在我的对象和字节流之间进行转换,或者这样的函数是否已经存在。或者如果我只是在我的代码中做错了什么。注释行是在取消注释时给我一个段错误的行......

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <iostream>
#include <unistd.h>

struct Record {
char c;
int i;
// float f;
// double d;
// char cs[32];
// boost::interprocess::string is;
// std::vector<int> v;

Record() {}
Record(int _k) { Init(_k); }

void Init(int _k = 0) {
c = _k + 65;
i = _k;
// f = _k + _k/100.0;
// d = _k + _k/1000.0;
// is = "interprocess string";
// for(int j = 0; j < _k; ++j) v.push_back(j);
}
};

int main(int argc, char *argv[])
{
using namespace boost::interprocess;
using std::cerr;
using std::endl;

int nObjects = 0;
size_t blockSize = 1024;

static std::string sharedObjName("MySharedMemory"); // why static?

const int writer = 1, reader = 2, error = -1;
int operation = error;

if(argc >= 2) {
if(argv[1][0] == 'w') operation = writer;
if(argv[1][0] == 'r') operation = reader;
}
if(argc == 1) operation = writer;

if(operation == writer) // Writer process
{
cerr << "Number of objects to write = ";
std::cin >> nObjects;

// Remove shared memory on construction and destruction
struct shm_remove {
shm_remove() { shared_memory_object::remove(sharedObjName.c_str()); }
~shm_remove(){ shared_memory_object::remove(sharedObjName.c_str()); }
} remover;

shared_memory_object shm(create_only, sharedObjName.c_str(), read_write);

shm.truncate(blockSize);

mapped_region region(shm, read_write);

offset_t shmSize;
shm.get_size(shmSize);

// Produce and write data
Record *pData0 = static_cast<Record*>(region.get_address());
Record *pData = pData0;

for(int i = 0; i < nObjects; ++i) {
if(pData0 + blockSize - pData < signed(sizeof(Record))) {
cerr << "Error: memory block full!" << endl;
break;
}
pData->Init(i);
pData += sizeof(Record);
}

//Launch child process
pid_t pId = fork();

if(pId == 0)
{
std::string s(argv[0]); s += " r";

if(std::system(s.c_str()) != 0) {
cerr << "Error launching reader process." << endl;
exit(1);
}
exit(0);
}
else if(pId > 0)
{
sleep(2);
cerr << "Writer has finished!" << endl;
}
else // pId < 0
exit(-1);
}
else if(operation == reader) // Reader process
{
shared_memory_object shm (open_only, sharedObjName.c_str(), read_only);

mapped_region region(shm, read_only);

Record *pData = static_cast<Record*>(region.get_address());

for(int i = 0; i < nObjects; ++i) {
// Print pData...
pData += sizeof(Record);
}
}
else
exit(1);

return 0;
}

感谢您的任何提示!

MacOS X 10.6.2 -海湾合作委员会 4.2 - boost 1.41.0

最佳答案

pData += sizeof(Record);

那一行就是问题所在。指针算术意味着更改以底层指针类型的“单位”为单位,在本例中为 Record。所以如果你想递增到下一条记录,你应该做 pData++,而不是 pData += sizeof(Record),这将使指针增加 64 字节(假设 sizeof(Record) 是 8 - 8*8 = 64)。

您在大小检查中遇到了类似的指针算术错误:

pData0 + blockSize - pData < signed(sizeof(Record))

你可能想要这样的东西:

blockSize/sizeof(Record)-(pData-pData0) <= 0

关于c++ - 使用不同于 char 的类型 boost shared_memory_object 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2010215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com