C - 2D dynamic array (double pointer) - shared memory

I have 2 processes (client and server) that exchange data through shared memory.

I need to create a 2D array that is dynamic (based on parameters). The array is stored in the structure and then written to the common segment.

I can write an array to shared memory, but I cannot extract it from another process.

Client code:

struct shared_use_st { int written_by_you; int **PID_PRI_array; }; /* Prepare Dynamic 2D array */ data_store = malloc(/*ROWS*/ 5 * sizeof(int*)); for(i=0;i<5; i++) data_store[i] = malloc(/*COLS*/ 2 * sizeof(int)); /* Prepare Dynamic 2D array - Shared Memory Seg */ shared_stuff->PID_PRI_array = malloc(/*ROWS*/ 5 * sizeof(int*)); for(i=0;i<5; i++) shared_stuff->PID_PRI_array[i] = malloc(/*COLS*/ 2 * sizeof(int)); /* Write PID and PRI to data_store array */ data_store[0][0] = pid; data_store[0][1] = 1; data_store[1][0] = 12345; data_store[1][1] = 2; data_store[2][0] = 12346; data_store[2][1] = 3; data_store[3][0] = 12347; data_store[3][1] = 4; data_store[4][0] = 12348; data_store[4][1] = 5; for(i=0;i<5;i++){ for(x=0;x<=1;x++){ shared_stuff->PID_PRI_array[i][x] = data_store[i][x]; } } 

Server Code:

  for(i=0;i<5;i++){ printf("PID: %d, PRI:%d\n", shared_stuff->PID_PRI_array[i][0], shared_stuff->PID_PRI_array[i][1]); } 

I get a "Segmentation Error" error.

Thanks.

+6
source share
1 answer

Even if your shared_stuff object is in shared memory, you are not writing an array to shared memory. You allocate space using malloc , writing data to this space, and then placing pointers in this space in shared_stuff . malloc allocates space in the regular address space of the current process, and not in the shared memory segment that you created. You need to write the contents of the array to shared memory.

Assuming that there is enough space for the array in the shared memory segment, you will have to manage the addresses yourself without using malloc . (If there is not enough space, you should make the shared memory segment larger or pass information to pieces over time.)

You can put a variable-length array into a shared memory segment as follows.

First, define a structure that contains all the management information you need, for example, the dimensions of the array:

 struct StuffStruct { size_t NumberOfRows, NumberOfColumns; … Other information as desired. }; 

Create a pointer to this structure and configure it on the shared memory segment:

 struct StuffStruct *Stuff = shm; // shm contains the address from shmat, performed previously. 

Create a pointer to an array with the desired number of columns and set it to point to the shared memory segment after the initial structure:

 int (*data_store)[NumberOfColumns] = (int (*)[NumberOfColumns]) ((char *) Stuff + sizeof *Stuff); 

(Note for C purists: Yes, the C standard does not guarantee what happens when you do pointer arithmetic like this. However, any implementation that supports shared memory must support this type of pointer arithmetic.)

Note that sizeof *Stuff + NumberOfRows * NumberOfColumns * size(int) should be no larger than the size of the shared memory segment. Otherwise, you will go over the shared memory segment in the next step.

For the next step, fill the array with data: assign values ​​to the data_store elements as for a normal two-dimensional array.

On the server, install Stuff in the same way. Then, after the client wrote the shared memory segment, read the row and column numbers from Stuff . Then set data_store in the same way. Then read from data_store .

+5
source

All Articles