I want to create a 2D array of char and share it through shared memory, but when I try to write in my shared memory I have segmentation fault, what can I do to write a string in my array
int main {
int j;
char** shm = malloc(30 * sizeof(char *));
for (int index=0;index<30;++index)
{
shm[index] =malloc(100 * sizeof(char));
}
int sh_id;
int i;
size_t sizeMatrix = sizeof_dm(30,100,sizeof(char));
//creation ou ouverture de la file de segment partag�e
key_t keyfile = ftok("keyFileIpc.txt",10);
if((sh_id = shmget(keyfile,sizeMatrix,IPC_CREAT|0666)) == -1) {
perror("shmget");
exit(1);
}
//on attache le segment au shm
if ((void*)(shm =(char**)shmat(sh_id, NULL, 0)) == (void *) -1) {
perror("shmat");
exit(1);
}
//i can't write in here and i don't know why
for (j=0;j<30;j++)
{
strcpy(shm[j],"i'm here");
}
printf("here\n");
return 0;
}
shm =(char**)shmat(...)reassignsshm, making you lose the earlier allocations and give you a memory leak.shmat(...)would myshmstill be in my shared memory ?shm =(char**)shmat(...)setsshmto point to some pointers that have been stored by another process. Those pointers have addresses in that other process’ address space. They are generally not valid in this process’ address space.