共享内存的std::string给分割错误(linux)
shared memory of std::string give segmentation fault (linux)
我目前正在linux上的2个进程之间的共享内存中尝试放置结构。我没有问题共享bool或int,但当试图共享字符串,std::string或char我有一个分割错误错误。
现在我的代码是:
#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;
}
在这个例子中,从两个结构体中共享两个bool值工作得很好,但是如果我尝试从std::string (etat, carte)中输入数据或读取数据,就像这样:
PrisesArray[1].etat="hello";
它在调试中给了我一个分段错误(在发布中明确不工作),我尝试了简单的字符串和字符(甚至一个字符),它仍然给了我一个分段错误。
是我错过了什么,当谈到文本共享或犯了一个错误在这里?
它给了我一个分段错误在调试(和明确不工作释放),我尝试了简单的字符串和字符(甚至一个字符)和它仍然给我一个分割错误。
这是因为std::string不是POD (Plain Old Data)类型。string在后台执行动态内存分配(使用new),当序列化它时(例如到共享内存或文件),只有指针被序列化。当反序列化(从共享内存或文件)到原始指针的内存可能不再存在时,这会使反序列化的字符串无法使用。
您必须编写一个函数,专门将字符串显式地序列化到共享内存中,就像标准操作符std::ostream operator>>(std::ostream&
您必须共享与std::字符串相关的数据,即
//服务器部分:
struct ShmData{
`...`
char chAux[1024];
};
int main()
{
...
shared_memory_object shm (create_only, "MyMem", read_write);
shm.truncate(offset_t(sizeof(ShmData)) );
mapped_region region(shm, read_write);
ShmData *shmData = (ShmData*)(region.get_address());
...
std::string str( "some stuff over the server" );
...
strcpy( shmData->chAux, str.c_str() );
...
}
//on client, the same struct and:
int main()
{
shared_memory_object shm (open_only, "MyMem", read_write);
mapped_region region(shm, read_write);
ShmData *shmData = (ShmData*)(region.get_address());
std::string strLocal(shmData->chAux); //assign to a local string the shared data
std::cout << "show strLocaln";
std::cout << strLocal << std::endl;
....
}
相关文章:
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- 这些是什么样的错误?即使我不在 Linux 上工作,我也遇到了 Linux 错误
- Arch Linux.AUR 包 mysql 不能用 makepkg 构建.错误:构建 () 中出现故障
- 分段 Linux Ubuntu 中的 g++ 错误,但在 Windows 中的 g++/MingW 中,在 C++ 中打
- [longjmp/setjmp]c++ 相同的代码窗口有异常 Linux 没有错误并且运行良好
- 在 Linux 中使用 -fvisibility=hidden 编译时出现库加载错误
- g++ 9.2.1 (Linux) 会导致 seg 错误,但 Windows 上的代码块不会
- 调试模板时出现问题.专门针对 Linux GCC 7、GCC 6、GCC 5、GCC 4.9 错误构建失败:模板参数 1
- LINUX 操作系统上的错误:令牌之前预期的构造函数、析构函数或类型转换'('?
- 从 Linux OMI (omicli) 到 Windows WMI 的连接失败,并出现与 DMTF 相关的错误
- C++ Linux 运行时basic_string::_M_construct null 无效错误
- Stringstream编译错误仅发生在linux环境中
- 如何在长g++错误消息上使用linux less命令
- curl_easy_在Linux上执行分段错误
- 在linux上使用g++输出错误,在windows上更正
- C++新手,得到编译错误(Linux,G ++编译器)
- Android NDK示例构建错误linux
- 共享内存的std::string给分割错误(linux)
- c++ Sockets -客户端给出分段错误(linux)
- C++提升日志记录编译错误 (Linux)