linux 共享内存 出错,共享内存的std :: string给出了分段错误(linux)

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

现在我的代码是:

#include

#include //shmat

#include

#include //open

#include

#include //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)读取数据,如下所示:

PrisesArray[1].etat="hello";

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

在文本共享或在这里犯错时我错过了什么吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值