【发布时间】:2016-01-03 21:17:20
【问题描述】:
我想在共享内存中创建字符串。我有简单的 C++ 程序:
#include<iostream>
#include<string>
#include<stdlib.h>
#include<semaphore.h>
#include<stdio.h>
#include<sys/shm.h>
int main(void){
std::string* line;
key_t lineKey = ftok("/tmp", '1');
int sharedLine = shmget(lineKey, sizeof(std::string), IPC_CREAT | 0660);
line = (std::string*)shmat(sharedLine, NULL, 0);
std::string helpVar = "";
while (true) {
std::cin >> helpVar;
(*line) = helpVar;
}
return 0;
}
但是当我执行它时(当执行g++ -o myprogram myprogram.cpp -lpthread 时编译正常)并写了一些它说Core dumped 的东西。我哪里错了?
【问题讨论】:
标签: c++ posix shared-memory