c++ - 无法访问共享内存中的字符串数组
问题描述
我正在尝试通过共享内存将文本行从编写器程序发送到阅读器程序。作家工作完美。但是,在阅读器程序的执行过程中,我在以下行出现分段错误
cout<<buffer->str[initial]<<endl;
当我在评论上述行时同时运行阅读器和编写器时,共享结构的计数值的变化是可见的并且正确递增。因此,根据我的诊断,问题在于访问共享结构的字符串数组。为什么会这样?
该代码由下面提供的 writer.cpp 和 reader.cpp 组成以供参考。
writer.cpp 的目的是从输入文件中读取行,然后将它们写入共享内存。
编写器.cpp
// for c++ related functions
#include<iostream>
#include<fstream>
#include<string>
// for c related functions
#include<cstdio>
#include<cstdlib>
#include<unistd.h>
// for system calls
#include<sys/types.h>
#include<sys/ipc.h>
// for shared memory
#include<sys/shm.h>
using namespace std;
// store the key for accessing the shared memory
key_t shm_key = ftok("practical",65);
// the structure for shared memory
struct shared{
bool the_end = false;
int count = 0;
string str[6];
};
int main(){
// create the shared memory
int shmid = shmget(shm_key,sizeof(shared),0666|IPC_CREAT);
if(shmid == -1){
perror("Error in creating shared memory");
return 1;
}
// attach server to the shared memory
struct shared *buffer = (shared*)shmat(shmid,NULL,0);
if(buffer == (void *) -1){
perror("Unable to attach to shared memory");
return 1;
}
// open the file for reading the input
ifstream fin("input.txt");
// check for errors in opening the file
if(!fin){
cerr<<"Error in opening the file"<<endl;
exit(1);
}
// to store the read line
string inptxt;
cout<<"Reading contents from file\n"<<endl;
while(fin){
// read a line
getline(fin,inptxt);
// exit the loop if string is empty
if(inptxt == "\0") break;
// store the line in the buffer
buffer->str[buffer->count++] = inptxt;
cout<<"This was written : "<<buffer->str[buffer->count-1]<<endl;
sleep(3);
}
cout<<"Closing the file"<<endl;
fin.close();
// marks the end of writing into shared memory
buffer->the_end = true;
if(shmdt(buffer) == -1){
perror("error in deatching from shared memory");
return 1;
}
if(shmctl(shmid,IPC_RMID,0) == -1){
perror("error in destroying the shared memory");
return 1;
}
cout<<"Writing Completed"<<endl;
return 0;
}
阅读器程序的目的是从共享内存中读取行,然后将它们显示在控制台上。
阅读器.cpp
// for c++ related functions
#include<iostream>
#include<fstream>
#include<string>
// for c related functions
#include<cstdio>
#include<cstdlib>
#include<unistd.h>
// for system calls
#include<sys/types.h>
#include<sys/ipc.h>
// for shared memory
#include<sys/shm.h>
using namespace std;
// store the key for accessing the shared memory
key_t shm_key = ftok("practical",65);
// the structure for shared memory
struct shared{
bool the_end = false;
int count = 0;
string str[6];
};
int main(){
// create the shared memory
int shmid = shmget(shm_key,sizeof(shared),0666|IPC_CREAT);
if(shmid == -1){
perror("Error in creating shared memory");
return 1;
}
// attach server to the shared memory
struct shared *buffer = (shared*)shmat(shmid,NULL,0);
if(buffer == (void *) -1){
perror("Unable to attach to shared memory");
return 1;
}
cout<<"Reading contents from shared memory\n"<<endl;
int initial = 0;
while(!buffer->the_end){
if(buffer->count > initial){
cout<<"initial : "<<initial<<" buffer count : "<<buffer->count<<"\n";
// the following line gives segmentation fault
// cout<<buffer->str[initial]<<endl;
initial++;
}
}
if(shmdt(buffer) == -1){
perror("error in deatching from shared memory");
return 1;
}
cout<<"Reading Completed"<<endl;
return 0;
}
解决方案
推荐阅读
- php - php psalm 配置和语法错误报告
- android - 片段不使用屏幕的全高,但仅在从另一个活动返回后
- google-sheets - 如何从一个单元格中的连接字符串中获取数据到 Google 表格中的另一个表格?
- machine-learning - 我需要多个模型进行交叉验证吗?
- r - 在 R 中的数据框中转换多个工作表
- python - 如何通过 filedialog.asksaveasfile() 保护 pdf 实例 - Tkinter python
- node.js - TypeError:无法读取 null 的属性“电子邮件”
- python - 优化 Keras CNN
- javascript - 查询选择器的更短方法?
- c# - 如何使用 C# 获取连接到 PC 的 SD 卡的驱动器号