Pointers inside shared memory segment

前端 未结 2 554
轻奢々
轻奢々 2020-12-06 11:39

I\'ve been trying this for hours, and google all the things I kind think of, but I\'m going crazy.

I have a struct:

typedef struct {
  int rows;
  in         


        
2条回答
  •  死守一世寂寞
    2020-12-06 12:07

    ctrl = (mem *)shmat(shmemid, 0, 0); 
    

    This only assigns valid memory to the ctrl pointer, not to ctrl->mat or ctrl->IDs_row.

    You probably want:

    mem *ctrl;
    shmemid = shmget(KEY, sizeof(ctrl), IPC_CREAT | 0666);
    //allocate memory for the structure
    ctrl = (mem *)shmat(shmemid, 0, 0);
    
    //allocate memory for the int*
    shmemid = shmget(KEY,((i-1)*num_cons))*sizeof(int), IPC_CREAT | 0666);
    ctrl->mat = (int*)shmat(shmemid, 0, 0);
    
    //allocate memory for the char*
    shmemid = shmget(KEY,i*26*sizeof(char), IPC_CREAT | 0666);
    ctrl->IDs_row = (char*)shmat(shmemid,0,0);
    

提交回复
热议问题