Is it possible to allocate a 2D array as shared memory using IPC?

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 to the matrix, I get a segment error.

+3
source share
2 answers

int ** is not a 2D array, but rather an array of pointers. Pointers should not be stored in shared memory, since a shared memory segment can be allocated to different addresses in different processes. Try using a simple, flat 1D array that will "emulate" a 2D array with some index magic, i.e.

x,y -> y*width+x
+13
source

- , . , . , OS, - , , .

+3

Source: https://habr.com/ru/post/1726587/


All Articles