C - shared memory - dynamic array inside shared struct

前端 未结 5 1675
攒了一身酷
攒了一身酷 2020-12-09 17:50

i\'m trying to share a struct like this
example:

typedef struct {
    int* a;
    int b;
    int c;
} ex;

between processes, the prob

5条回答
  •  执念已碎
    2020-12-09 18:31

    Convert the struct:

    typedef struct {
         int b;
         int c;
         int a[];
    } ex;
    

    and then on parent process:

    int mid = shmget(key, sizeof(ex) + arraysize*sizeof(int), 0666);
    

    it should work.

    In general, it is difficult to work with dynamic arrays inside structs in c, but in this way you are able to allocate the proper memory (this will also work in malloc: How to include a dynamic array INSIDE a struct in C?)

提交回复
热议问题