I want to allocate shared memory as a 2D array using IPC. I tried the following:
id_shmem = shmget(ipc_key, sizeof(int)*rows*columns, IPC_CREAT|0666);
matrix = (int **)shmat(id_shmem, 0, 0);
The problem is that whenever I try to write something into the matrix, I get a segment fault.
int** is not 2D array, it is rather an array of pointers. You should not store pointers in shared memory, as shared memory segment may be allocated at different addresses in different processes. Try to use simple, flat 1D array, which will “emulate” 2D array with some index magic, ie.