共享内存段内的指针 [英] Pointers inside shared memory segment
问题描述
我一直想这几个小时,而谷歌所有的事情我想那种,但我要疯了。
我有一个结构:
typedef结构{
诠释行;
INT collumns;
INT *垫;
字符* IDs_row;
} MEM;
我不知道为int *(矩阵)和char *的大小,直到后来。
当我这样做,我创建共享内存是这样的:
存储* CTRL;
INT大小=(2 +((I-1)* num_cons))*的sizeof(int)的+ I * 26 *的sizeof(char)的; //我现在有真正的大小
shmemid = shmget的(KEY,大小,IPC_CREAT | 0666);
如果(shmemid℃,){
PERROR(哈fallado拉creacion德拉MEMORIA compartida。);
出口(1);
}
CTRL =(MEM *)的shmat(shmemid,0,0);
如果(按Ctrl< =(MEM *)(0)){
PERROR(哈fallado埃尔acceso一个MEMORIA compartida);
出口(2);
}
这里没有问题。然后,我给一个值CTRL->行和collumns,并分配0至所有矩阵。
但在那之后,我写了char *与BAM,一些分段错误。
调试程序,我看到两个指针,垫和IDs_row了空。我怎么给他们共享内存段??
里面的正确的价值观我试图消除的char *指针,只给它一个尝试,然后分段错误是在连接到所述共享存储器的其他程序,只是检查了矩阵(内部的价值观检查 - >行 - > collumns是succesfull)
CTRL =(MEM *)的shmat(shmemid,0,0);
这仅分配有效的内存到 CTRL
指针,而不是 CTRL->垫
或 CTRL-> IDs_row
您可能希望:
存储* CTRL;
shmemid = shmget的(KEY,sizeof的(CTRL),IPC_CREAT | 0666);
//为结构分配内存
CTRL =(MEM *)的shmat(shmemid,0,0);//为INT分配内存*
shmemid = shmget的(KEY,((I-1)* num_cons))*的sizeof(int)的,IPC_CREAT | 0666);
CTRL->垫子=为(int *)的shmat(shmemid,0,0);//为字符分配内存*
shmemid = shmget的(KEY,我* 26 * sizeof的(炭),IPC_CREAT | 0666);
CTRL-> IDs_row =(字符*)的shmat(shmemid,0,0);
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;
int collumns;
int* mat;
char* IDs_row;
} mem;
I don't know the sizes of the int* (a Matrix) and char* untill later.
When I do, I create the shared memory like this:
mem *ctrl;
int size = (2 + ((i-1)*num_cons))*sizeof(int) + i*26*sizeof(char); //I have the real size now
shmemid = shmget(KEY, size, IPC_CREAT | 0666);
if (shmemid < 0) {
perror("Ha fallado la creacion de la memoria compartida.");
exit(1);
}
ctrl = (mem *)shmat(shmemid, 0, 0);
if (ctrl <= (mem *)(0)) {
perror("Ha fallado el acceso a memoria compartida");
exit(2);
}
No problem here. Then I give a value to ctrl->rows and collumns, and assign 0 to all the matrix.
But after that, I write something in the char* and bam, segmentation fault.
Debugging the program I saw that both pointers, mat and IDs_row where null. How do I give them the correct values inside the shared memory segment??
I tried removing the char* pointer, just to give it a try, and then the segmentation fault error was in the other program that connected to said shared memory and just checked the values inside the matrix (checking ->rows and ->collumns was succesfull)
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);
这篇关于共享内存段内的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!