我在C中有一個鏈接列表,或者我想。 的結構是:如何在共享內存的struct中分配鏈表c
//Structure of the domain list
typedef struct domains *domain_list;
struct domains{
char *domain;
domain_list next;
}domains_node;
//Structure of the configuration of the server
typedef struct{
int n_threads;
domain_list domain_list;
char* local_domain;
char* named_pipe_statistics;
}server_config;
我試圖在共享內存進入其中,我敢肯定,該結構是好的,但我不知道是否該鏈表是正確的(使用全局變量):
//Initialize shared memory
if((config_shmid = shmget(IPC_PRIVATE, sizeof(server_config), IPC_CREAT|0777)) < 0){
perror("Error in config shmid\n");
exit(1);
}
if((config = (server_config*)shmat(config_shmid, NULL, 0)) == (server_config *)-1){
perror("Error in config shmat\n");
exit(1);
}
if((config_domain_shmid = shmget(IPC_PRIVATE, sizeof(struct domains), IPC_CREAT|0777)) < 0){
perror("Error in domain_list config shmid\n");
exit(1);
}
if((config->domain_list = (domain_list)shmat(config_domain_shmid, NULL, 0)) == (domain_list)-1){
perror("Error in domain_list config shmat\n");
exit(1);
}
這是過程的交際。我需要一個動態的(不固定的)鏈接列表,在一個結構中,共享內存中。 所以,我需要的是一種爲我創建的新節點分配內存空間的方式,以及如何將它們連接起來。我現在不是用malloc,但在這個問題上的答案只是作爲「足夠的分配」,我不知道它是什麼。
http://stackoverflow.com/q/27218618/992406 – houssam