How to write Jagged array into shared memory with C/C++

55 views Asked by At

I have the following jagged array (rank 6) which is output of a C/C++ code (this is achieved by splitting a rectangular 2D array)

{{{{{{1270529, 1, 1, 1, 1, 0, 1, 0.05, 1, 0., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}}}, 
{{{{10265858, 1, 2, 1, 1, 0, 2, 0.05, 700, 28000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}}, 
{{{13438466, 1, 2, 2, 1, 0, 2, 0.05, 700, 28000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}}}, 
{{{{37571586, 1, 3, 1, 1, 950, 3, 0.05, 700, 28000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{37571842, 1, 3, 1, 2, 950, 4, 0.05, 700, 28000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{37572098, 1, 3, 1, 3, 960, 3, 0.05, 700, 28000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{37572354, 1, 3, 1, 4, 960, 4, 0.05, 700, 28000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{37572610, 1, 3, 1, 5, 970, 3, 0.05, 700, 28000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{37572866, 1, 3, 1, 6, 970, 4, 0.05, 700, 28000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}}, 
{{{25125634, 1, 3, 2, 1, 950, 3, 0.05, 700, 28000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{25125890, 1, 3, 2, 2, 950, 4, 0.05, 700, 28000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{25126146, 1, 3, 2, 3, 960, 3, 0.05, 700, 28000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{25126402, 1, 3, 2, 4, 960, 4, 0.05, 700, 28000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{25126658, 1, 3, 2, 5, 970, 3, 0.05, 700, 28000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{25126914, 1, 3, 2, 6, 970, 4, 0.05, 700, 28000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}}}}, 
{{{{{779521, 2, 1, 1, 1, 0, 1, 0.05, 1, 0., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}}}, 
{{{{12509698, 2, 2, 1, 1, 0, 2, 0.05, 1500, 45000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}}, 
{{{13457154, 2, 2, 2, 1, 0, 2, 0.05, 1500, 45000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}}}, 
{{{{44526850, 2, 3, 1, 1, 565, 3, 0.05, 1500, 45000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{44527106, 2, 3, 1, 2, 565, 4, 0.05, 1500, 45000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{44527362, 2, 3, 1, 3, 570, 3, 0.05, 1500, 45000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{44527618, 2, 3, 1, 4, 570, 4, 0.05, 1500, 45000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{44527874, 2, 3, 1, 5, 575, 3, 0.05, 1500, 45000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{44528130, 2, 3, 1, 6, 575, 4, 0.05, 1500, 45000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}}, 
{{{43307010, 2, 3, 2, 1, 565, 3, 0.05, 1500, 45000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{43307266, 2, 3, 2, 2, 565, 4, 0.05, 1500, 45000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{43307522, 2, 3, 2, 3, 570, 3, 0.05, 1500, 45000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{43307778, 2, 3, 2, 4, 570, 4, 0.05, 1500, 45000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{43308034, 2, 3, 2, 5, 575, 3, 0.05, 1500, 45000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}, 
{{43308290, 2, 3, 2, 6, 575, 4, 0.05, 1500, 45000., 0.}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}}}}}

I need to write it into the shared memory.

A rectangular 2D array is written into the shared memory as follows

    int MemoryID = 1;
    int dimension[2]={60,11};
    SHMID = shmget(MemoryID, sizeof(int)*dimension[0]*dimension[1], 0666 | IPC_CREAT);
    if(SHMID == -1) 
    {
        std::cerr << "Error creating shared memory segment" << std::endl;
        return -1;
    }

    int rows = dimension[0];
    int cols= dimension[1];
    int (*arr)[cols];
    arr = (int(*)[cols]) shmat(SHMID, NULL, 0);  

Now arr can be accessed like this

    for(int i=0;i<rows;i++)
    {
        for(int j=0;j<cols;j++)
        {
            arr[i][j];
        }
    }

Can anyone let me know how to write the code (to store the above mention 6D jagged array into the shared memory) in C/C++?

0

There are 0 answers