如何在所有进程中共享一个结构体数组?

问题描述 投票:0回答:1

我有一个结构,它可能看起来像

typedef struct {
    char *data;
    int length;
} string;

然后我定义一个数组

string *my_data = (string *)malloc(sizeof(string) * 10);
for (int i = 0; i < 10; i++) {
  my_data[i].data = strdup(other_str);
  my_data[i].length = strlen(other_str);
}

我想将数组广播到所有进程,但我不知道该怎么做。所有数据都会从文件中读取,所以我无法将

char *data
更改为
char data[MAX_LEN]
,如果这里有一些好的方法?非常感谢!

我发现MPI有共享内存,所以我尝试使用共享内存,但也得到了

Signal: Segmentation fault (11)

c mpi
1个回答
0
投票

您可以使用链表来保留动态malloc内存,因为您已经知道长度。希望对你有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.