std::string 的共享内存给出分段错误(linux) [英] shared memory of std::string give segmentation fault (linux)

查看:39
本文介绍了std::string 的共享内存给出分段错误(linux)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 linux 上的 2 个进程之间的共享内存中尝试放置结构.我共享 bool 或 int 没有问题,但在尝试共享字符串、std::string 或 char 时出现分段错误.

I am currently trying the put structures in a shared memory between 2 process on linux. I have no problem sharing bool or int but when trying to share a string, std::string or char i have a segmentation fault error.

现在我的代码是:

#include <iostream>
#include <sys/types.h> //shmat
#include <sys/shm.h>
#include <sys/stat.h> //open
#include <fcntl.h>
#include <unistd.h> //close

using namespace std;

struct Prises{

int numero;
int transactionId;
bool reservation;
bool charge;
bool disponibilite;
bool defaut;
bool verrouillage;
bool trappe;
int LEDverte;
int LEDrouge;
std::string carte;
std::string etat;

};

int main()
{
const char *keyFile = "/tmp/key.dat";
/* Make sure the file exists. */
int descriptor = open(keyFile, O_CREAT | O_RDWR, S_IRWXU);

/* Only wanted to make sure that the file exists. */
close(descriptor);

/* Generate memory key. */
key_t sharedKey = ftok(keyFile, 1);

/* Get the shared memory segment id. Note we include
   the permissions. */
int sharedSpaceId = shmget(sharedKey, 2*sizeof(Prises),
    IPC_CREAT | S_IRUSR | S_IWUSR);

/* Attach the shared memory segment. */
Prises *PrisesArray = (Prises *) shmat(sharedSpaceId, NULL, 0);

PrisesArray[1].defaut=true;
PrisesArray[2].defaut=false;

int ok;
std::cin>>ok;
return 0;
}

在此示例中,共享 2 个结构中的 2 个 bool 运行良好,但是如果我尝试输入数据或从 std::string (etat, carte) 中读取数据,如下所示:

In this example sharing the 2 bool from the 2 structures is working well but if i try to input a data or read a data from the std::string (etat, carte) like this :

PrisesArray[1].etat="hello";

它在调试中给了我一个分段错误(并且明确在发布中不起作用),我尝试使用简单的字符串和字符(甚至一个字符),但它仍然给我一个分段错误.

It gives me a segmentation fault in debug (and clear don't work in release), i tried with simple string and char (even one char) and it still gives me a segmentation fault.

在文本共享方面我是否遗漏了什么或在这里犯了错误?

Am i missing something when it comes to text sharing or making a mistake here ?

推荐答案

它在调试中给了我一个分段错误(并且明确不起作用)发布),我尝试使用简单的字符串和字符(甚至一个字符),然后它仍然给我一个分段错误.

It gives me a segmentation fault in debug (and clear don't work in release), i tried with simple string and char (even one char) and it still gives me a segmentation fault.

这是因为 std::string 不是 POD(Plain Old Data)类型.std::string 在幕后执行动态内存分配(使用 new),并且在对其进行序列化(例如共享内存或文件)时,仅对指针进行序列化.当将它(从共享内存或文件)反序列化时,原始指针的内存可能不再存在,这会导致反序列化的字符串无法使用.

This is because std::string is not a POD (Plain Old Data) type. std::string performs dynamic memory allocation (using new) behind the scenes, and when serializing it (e.g to shared memory or file), only the pointers are serialized. When deserializing it (from shared memory or file) memory to the original pointers are likely to not exist anymore, and this renders the deserialized string unusable.

您必须编写一个专门将字符串显式序列化到共享内存的函数,就像标准运算符 std::ostream 运算符 >>(std::ostream& stream, const std::string&)

You would have to write a function that specifically serializes the string explicitly to your shared memory, as is the case with the standard operator std::ostream operator >>(std::ostream& stream, const std::string&)

这篇关于std::string 的共享内存给出分段错误(linux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆