简体   繁体   English

C - 2D动态数组(双指针) - 共享内存

[英]C - 2D Dynamic Array (Double Pointer) - Shared Memory

I've got 2 processes (Client and Server) that are communicating through shared memory. 我有两个通过共享内存进行通信的进程(客户端和服务器)。

I need to create a 2D Array that is Dynamic (based on parameters). 我需要创建一个动态的2D数组(基于参数)。 The array is stored in a struct and then written to the shared segment. 数组存储在结构中,然后写入共享段。

I can write the array to the shared memory, but cannot retrieve it from the other 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 Fault" error. 我收到“分段错误”错误。

Thanks. 谢谢。

Even if your shared_stuff object is in shared memory, you are not writing the array to shared memory. 即使您的shared_stuff对象位于共享内存中,也不会将该数组写入共享内存。 You are allocating space with malloc , writing data to that space, and then putting pointers to that space into shared_stuff . 您正在使用malloc分配空间,将数据写入该空间,然后将指向该空间的指针放入shared_stuff malloc allocates space within the current process' normal address space, not in a shared memory segment you have created. malloc在当前进程的正常地址空间内分配空间,而不是在您创建的共享内存段中。 You need to write the array contents to the shared memory. 您需要将数组内容写入共享内存。

Presuming there is enough space for the array within the shared memory segment, you will have to manage the addresses yourself, not using malloc . 假设共享内存段中有足够的空间用于阵列,则必须自己管理地址,而不是使用malloc (If there is not enough space, you must make the shared memory segment larger or convey the information in pieces over time.) (如果没有足够的空间,则必须使共享内存段更大,或者随着时间的推移将信息分段传送。)

You can place a variable-length array within the shared memory segment as follows. 您可以将可变长度数组放在共享内存段中,如下所示。

First, define a structure that contains all the “management” information you need, such as the array sizes: 首先,定义一个包含所需“管理”信息的结构,例如数组大小:

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

Create a pointer to that structure and set it to point to 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 into 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 providing shared memory support must provide support for this sort of pointer arithmetic.) (对于C纯粹主义者的注意事项:是的,C标准并不保证当您像这样执行指针算法时会发生什么。但是,任何提供共享内存支持的实现都必须支持这种指针算法。)

Note that sizeof *Stuff + NumberOfRows * NumberOfColumns * size(int) must be no greater than the size of the shared memory segment. 请注意, sizeof *Stuff + NumberOfRows * NumberOfColumns * size(int) 必须不大于共享内存段的大小。 Otherwise you will overrun the shared memory segment in the next step. 否则,您将在下一步中超出共享内存段。

For the next step, fill the array with data: Assign values to the elements of data_store as for a normal two-dimensional array. 对于下一步,使用数据填充数组:将值分配给data_store的元素, data_store普通的二维数组一样。

In the server, set Stuff the same way. 在服务器中,以相同的方式设置Stuff Then, after the client has written the shared memory segment, read the numbers of rows and columns from Stuff . 然后,在客户端编写共享内存段后,从Stuff读取行数和列数。 Then set data_store the same way. 然后以相同的方式设置data_store Then read from data_store . 然后从data_store读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM