我目前正在尝试在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";
它在调试中给了我一个分段错误(并且明确在发布时不起作用),我尝试使用简单的字符串和字符(甚至一个字符),它仍然给我一个分段错误.
在文本共享或在这里犯错时我错过了什么吗?