我有兩個文件,我試圖通過共享內存IPC。我在兩個文件中使用類似的語句進行分配。在服務器文件:C內存映射結構數組泄漏
int fd;
int size = MAX_LEN;
int bigSize = sizeof(struct region)+ size * sizeof(struct client_message) + size * sizeof(struct server_message);
struct region *rptr = (struct region*)malloc(bigSize);
printf("region size: %d clientMessage size: %d serverMessage size: %d and the total size: %d\n", sizeof(struct region), size * sizeof(struct client_message), size * sizeof(struct server_message), bigSize);
fd = shm_open("/myregion", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if (ftruncate(fd, bigSize) == -1)
printf("error creating ftruncate\n");
rptr = mmap(NULL, sizeof(struct region),
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
我用下面的鏈接中的示例動態地分配共享存儲器: C Windows - Memory Mapped File - dynamic array within a shared struct
當我在此服務器while循環和增加Ĵ分配struct client_message {
pthread_t client_id;
int question;
};
struct server_message {
pthread_t client_id;
pid_t server_id;
int answer;
};
struct region { /* Defines "structure" of shared memory */
int len;
struct client_message ptr_client_message[0];
struct server_message ptr_server_message[0];
};
文件,
(rptr->ptr_client_message[(j)%size]).question = 30;
(rptr->ptr_server_message[(j)%size]).answer = 20;
我從客戶端文件中讀取它是這樣:
printf("rptr len is %d and question of client %d is: %d, answer of server is %d \n", size, k%size, (rptr->ptr_client_message[(k)%size]).question, (rptr->ptr_server_message[(k)%size]).answer);
輸出被mindboggling: 從服務器終端獲得:
rptr len is 10 and question of client 0 is: 30, answer of server is 20
rptr len is 10 and question of client 1 is: 30, answer of server is 20
rptr len is 10 and question of client 2 is: 30, answer of server is 20
...
改變用於client_message陣列的10個元素,即,高達客戶端[MAX_LEN]
從客戶終端我得到:
rtpr len is 10 and question of client 0 is: 30, answer of server is 20
rptr len is 10 and question of client 1 is: 30, answer of server is 30
rptr len is 10 and question of client 2 is: 30, answer of server is 20
rptr len is 10 and question of client 3 is: 30, answer of server is 30
rptr len is 10 and question of client 4 is: 30, answer of server is 20
rptr len is 10 and question of client 5 is: 30, answer of server is 30
rptr len is 10 and question of client 6 is: 30, answer of server is 20
rptr len is 10 and question of client 7 is: 30, answer of server is 20
rptr len is 10 and question of client 8 is: 30, answer of server is 20
rptr len is 10 and question of client 9 is: 30, answer of server is 20
rptr len is 10 and question of client 0 is: 30, answer of server is 20
rptr len is 10 and question of client 1 is: 30, answer of server is 30
rptr len is 10 and question of client 2 is: 30, answer of server is 20
rptr len is 10 and question of client 3 is: 30, answer of server is 30
rptr len is 10 and question of client 4 is: 30, answer of server is 20
rptr len is 10 and question of client 5 is: 30, answer of server is 30
rptr len is 10 and question of client 6 is: 30, answer of server is 20
rptr len is 10 and question of client 7 is: 30, answer of server is 20
rptr len is 10 and question of client 8 is: 30, answer of server is 20
因此,從另一個進程到達時,struct區域中的條目是混合的。我怎樣才能防止這一點?
結構中不能有2個靈活的數組成員。基本上'rptr-> ptr_client_message'和'rptr-> ptr_server_message'是別名。 –
謝謝,你可能會推薦任何改進?順便說一下,你怎麼知道這是肯定的,我的意思是任何c參考? –
http://port70.net/~nsz/c/c11/n1570.html#6.7.2.1p18:「作爲一種特殊情況,具有多個命名成員的結構的最後一個元素可能具有不完整的數組類型;這被稱爲靈活的陣列成員。「 –