共享内存中的 C++ 二维数组(基于命令行参数)
C++ two dimensional array in shared memory (based on command line argument)
我正在尝试将存在于共享内存段中的二维 (2D) 数组附加到 C 中的进程。我在编译时遇到以下错误:
main.cpp:52:22: error: invalid conversion from ‘void*’ to ‘int (*)[2]’ [-fpermissive]
shm_assemble = shmat(shmid_assemble,0,0);
编译命令为:
g++ main.cpp -o myworkshop
我的代码是这样的:
#include <iostream>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <ctype.h>
#include "myheader.h"
void perror_exit(char *message);
bool init(int argc, char * argv[], int *Y);
int main (int argc , char* argv[]){
if(argc<3) {
perror_exit("Usage: ./myworkshop -Y <value>\n");
return(-1);
}
int i=0,n=0,child_pid=0,Y=0,shmid_paint=0,shmid_assemble=0,shmid_check=0,shmid_y=0,painter_empty=0,painter_full=0,checker_full=0,checker_empty=0,assembler_full=0,assembler_empty=0,ex=0;
int *pids = new int[8];
kill_message *shm_y;
message *shm_paint,*shm_check;
int (*shm_assemble)[2];
if (!init(argc,argv,&Y)){
std::cout << "Something is wrong with the arguments, try again\n";
exit(-2);
}
if(Y==0) return 0; // if no products are to be created the program should exit
const int SHMSIZE = sizeof(message); // Shared Memory size = the size of a message
const int assembler_SHMSIZE = sizeof(int[3*Y][2]);
printf("assembler_SHMSIZE is: %d\n", assembler_SHMSIZE);
const int kill_SHMSIZE = sizeof(kill_message);
// srand(time(NULL));
/* --- Shared memory creation --- */
shmid_paint = shmget(IPC_PRIVATE, SHMSIZE, IPC_CREAT | 0666);
shmid_assemble = shmget(IPC_PRIVATE, assembler_SHMSIZE, IPC_CREAT | 0666);
shmid_check = shmget(IPC_PRIVATE, SHMSIZE, IPC_CREAT | 0666);
shmid_y = shmget(IPC_PRIVATE, kill_SHMSIZE, IPC_CREAT | 0666);
shm_paint = (message*)shmat(shmid_paint,NULL,0);
shm_assemble = (int **)shmat(shmid_assemble,0,0);
shm_check = (message*)shmat(shmid_check,NULL,0);
shm_y = (kill_message*)shmat(shmid
}
我做错了什么?
我读了这个 但我没有正确理解答案,或者静态大小对我的情况没有帮助。
我的目标是得到一个 [3*Y][2] 整数的二维数组,其中 Y 在 main 中作为参数给出,并在 init 函数中正确初始化,我没有在此处包含。
好的,答案很简单。
我必须将 shm_write_assembler 声明为 int **。我的初始 int (shm_write_assembler)[2] 在 C 中工作,就像我用作示例但不是在 C++ 中使用的相关问题一样。改变这个让我的代码工作起来很有魅力。
这是更正后的代码:
#include <iostream>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <ctype.h>
#include "myheader.h"
void perror_exit(char *message);
bool init(int argc, char * argv[], int *Y);
int main (int argc , char* argv[]){
if(argc<3) {
perror_exit("Usage: ./myworkshop -Y <value>\n");
return(-1);
}
int i=0,n=0,child_pid=0,Y=0,shmid_paint=0,shmid_assemble=0,shmid_check=0,shmid_y=0,painter_empty=0,painter_full=0,checker_full=0,checker_empty=0,assembler_full=0,assembler_empty=0,ex=0;
int *pids = new int[8];
kill_message *shm_y;
message *shm_paint,*shm_check;
int **shm_assemble;
if (!init(argc,argv,&Y)){
std::cout << "Something is wrong with the arguments, try again\n";
exit(-2);
}
if(Y==0) {
delete[] pids;
return 0; // if no products are to be created the program should exit
}
const int SHMSIZE = sizeof(message); // Shared Memory size = the size of a message
const int assembler_SHMSIZE = sizeof(int[3*Y][2]);
printf("assembler_SHMSIZE is: %d\n", assembler_SHMSIZE);
const int kill_SHMSIZE = sizeof(kill_message);
// srand(time(NULL));
/* --- Shared memory creation --- */
shmid_paint = shmget(IPC_PRIVATE, SHMSIZE, IPC_CREAT | 0666);
shmid_assemble = shmget(IPC_PRIVATE, assembler_SHMSIZE, IPC_CREAT | 0666);
shmid_check = shmget(IPC_PRIVATE, SHMSIZE, IPC_CREAT | 0666);
shmid_y = shmget(IPC_PRIVATE, kill_SHMSIZE, IPC_CREAT | 0666);
shm_paint = (message*)shmat(shmid_paint,NULL,0);
shm_assemble = (int**)shmat(shmid_assemble,NULL,0);
shm_check = (message*)shmat(shmid_check,NULL,0);
shm_y = (kill_message*)shmat(shmid_y,NULL,0);
/* --- Semaphore creation --- */
painter_empty = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);
painter_full = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);
checker_empty = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);
checker_full = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);
assembler_empty = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);
assembler_full = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);
delete[] pids;
}
我正在尝试将存在于共享内存段中的二维 (2D) 数组附加到 C 中的进程。我在编译时遇到以下错误:
main.cpp:52:22: error: invalid conversion from ‘void*’ to ‘int (*)[2]’ [-fpermissive]
shm_assemble = shmat(shmid_assemble,0,0);
编译命令为:
g++ main.cpp -o myworkshop
我的代码是这样的:
#include <iostream>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <ctype.h>
#include "myheader.h"
void perror_exit(char *message);
bool init(int argc, char * argv[], int *Y);
int main (int argc , char* argv[]){
if(argc<3) {
perror_exit("Usage: ./myworkshop -Y <value>\n");
return(-1);
}
int i=0,n=0,child_pid=0,Y=0,shmid_paint=0,shmid_assemble=0,shmid_check=0,shmid_y=0,painter_empty=0,painter_full=0,checker_full=0,checker_empty=0,assembler_full=0,assembler_empty=0,ex=0;
int *pids = new int[8];
kill_message *shm_y;
message *shm_paint,*shm_check;
int (*shm_assemble)[2];
if (!init(argc,argv,&Y)){
std::cout << "Something is wrong with the arguments, try again\n";
exit(-2);
}
if(Y==0) return 0; // if no products are to be created the program should exit
const int SHMSIZE = sizeof(message); // Shared Memory size = the size of a message
const int assembler_SHMSIZE = sizeof(int[3*Y][2]);
printf("assembler_SHMSIZE is: %d\n", assembler_SHMSIZE);
const int kill_SHMSIZE = sizeof(kill_message);
// srand(time(NULL));
/* --- Shared memory creation --- */
shmid_paint = shmget(IPC_PRIVATE, SHMSIZE, IPC_CREAT | 0666);
shmid_assemble = shmget(IPC_PRIVATE, assembler_SHMSIZE, IPC_CREAT | 0666);
shmid_check = shmget(IPC_PRIVATE, SHMSIZE, IPC_CREAT | 0666);
shmid_y = shmget(IPC_PRIVATE, kill_SHMSIZE, IPC_CREAT | 0666);
shm_paint = (message*)shmat(shmid_paint,NULL,0);
shm_assemble = (int **)shmat(shmid_assemble,0,0);
shm_check = (message*)shmat(shmid_check,NULL,0);
shm_y = (kill_message*)shmat(shmid
}
我做错了什么?
我读了这个
我的目标是得到一个 [3*Y][2] 整数的二维数组,其中 Y 在 main 中作为参数给出,并在 init 函数中正确初始化,我没有在此处包含。
好的,答案很简单。 我必须将 shm_write_assembler 声明为 int **。我的初始 int (shm_write_assembler)[2] 在 C 中工作,就像我用作示例但不是在 C++ 中使用的相关问题一样。改变这个让我的代码工作起来很有魅力。
这是更正后的代码:
#include <iostream>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <ctype.h>
#include "myheader.h"
void perror_exit(char *message);
bool init(int argc, char * argv[], int *Y);
int main (int argc , char* argv[]){
if(argc<3) {
perror_exit("Usage: ./myworkshop -Y <value>\n");
return(-1);
}
int i=0,n=0,child_pid=0,Y=0,shmid_paint=0,shmid_assemble=0,shmid_check=0,shmid_y=0,painter_empty=0,painter_full=0,checker_full=0,checker_empty=0,assembler_full=0,assembler_empty=0,ex=0;
int *pids = new int[8];
kill_message *shm_y;
message *shm_paint,*shm_check;
int **shm_assemble;
if (!init(argc,argv,&Y)){
std::cout << "Something is wrong with the arguments, try again\n";
exit(-2);
}
if(Y==0) {
delete[] pids;
return 0; // if no products are to be created the program should exit
}
const int SHMSIZE = sizeof(message); // Shared Memory size = the size of a message
const int assembler_SHMSIZE = sizeof(int[3*Y][2]);
printf("assembler_SHMSIZE is: %d\n", assembler_SHMSIZE);
const int kill_SHMSIZE = sizeof(kill_message);
// srand(time(NULL));
/* --- Shared memory creation --- */
shmid_paint = shmget(IPC_PRIVATE, SHMSIZE, IPC_CREAT | 0666);
shmid_assemble = shmget(IPC_PRIVATE, assembler_SHMSIZE, IPC_CREAT | 0666);
shmid_check = shmget(IPC_PRIVATE, SHMSIZE, IPC_CREAT | 0666);
shmid_y = shmget(IPC_PRIVATE, kill_SHMSIZE, IPC_CREAT | 0666);
shm_paint = (message*)shmat(shmid_paint,NULL,0);
shm_assemble = (int**)shmat(shmid_assemble,NULL,0);
shm_check = (message*)shmat(shmid_check,NULL,0);
shm_y = (kill_message*)shmat(shmid_y,NULL,0);
/* --- Semaphore creation --- */
painter_empty = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);
painter_full = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);
checker_empty = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);
checker_full = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);
assembler_empty = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);
assembler_full = semget(IPC_PRIVATE,1,IPC_CREAT | 0666);
delete[] pids;
}