【问题标题】:C++ two dimensional array in shared memory (based on command line argument) [closed]共享内存中的 C++ 二维数组(基于命令行参数)[关闭]
【发布时间】:2019-05-02 02:53:54
【问题描述】:

我正在尝试将一个存在于共享内存段中的二维 (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
}

我在这里做错了什么?

我阅读了此similar question,但我要么没有正确理解答案,要么静态大小对我的情况没有帮助。

我的目标是拥有一个由 [3*Y][2] 整数组成的二维数组,其中 Y 作为 main 中的参数给出,并在我未包括在此处的 init 函数中正确初始化。

【问题讨论】:

  • 我认为您不需要从shmat 转换返回值。
  • 您的代码和编译器是 C++(尽管使用了一些 C 库和习语)。您想在 C 中执行此操作,还是在 C++ 中执行此操作?
  • 后缀 .cpp 告诉编译器您有 C++ 源代码,因此它将 C++ 规则应用于该源代码。不要说假话——编译器不喜欢被骗,当你这样做时,他们会想方设法让自己回来。是的,它们是非常不同的语言,尤其是在内存管理领域。
  • C 是 C; C++是C++; C 风格的 C++ 仍然是 C++,即使它以特定方式使用 C++。有 negative 值来混淆它。不要假装它不重要,因为它很重要。
  • 好吧,你一定对此感到困惑。我编写 C++,使用 C 库并使用 g++ 编译,我的文件是 .cpp,但我在这里提到的这个问题与我使用的语言无关,所以为了简单起见,我编写并标记了 C。现在清楚了吗? @AnttiHaapala

标签: c++ ipc shared-memory


【解决方案1】:

好的,答案很简单。 我不得不将 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多