2017-10-10 143 views
0

我正在學校項目上工作,一切正常,但在編譯時,我得到了「指針和整數之間的比較」警告。誰能解釋爲什麼會發生這種情況?我在下面的所有四個共享內存初始化中都得到相同的警告。我嘗試了不同的方法進行比較。據我所知,shmget和shmat在錯誤時應該返回-1。即使警告出現了,代碼也能很好地工作。我做了更改以提示錯誤。這些警告在shmat線上提示。下面列出的最小代碼。共享內存 - 警告:指針和整數之間的比較

//-------------------------------------------------- 
//Variables 
//-------------------------------------------------- 

//Shared memory variables 
int (*clockVar)[2]; 
int *turn; 
int (*shmMsg)[2]; 
enum state {idle, want_in, in_cs, done, dne} *flag; 
long *pidList; 

//Shared memory keys 
key_t clockKey; 
key_t turnKey; 
key_t msgKey; 
key_t flagKey; 
key_t pidKey; 

//Shared memory IDs 
int clockID = 0; 
int turnID = 0; 
int msgID = 0; 
int flagID = 0; 
int pidID = 0; 
//-------------------------------------------------- 
//Key Initialization 
//-------------------------------------------------- 

clockKey = ftok("ftok_clock", 13); 
if (clockKey == -1){ 
    perror("Clock: Failed to load ftok file"); 
    return 1; 
} 

msgKey = ftok("ftok_msg", 17); 
if (msgKey == -1){ 
    perror("Message: Failed to load ftok file"); 
    return 1; 
} 

flagKey = ftok("ftok_flag", 15); 
if (flagKey == -1){ 
    perror("Flag: Failed to load ftok file"); 
    return 1; 
} 

pidKey = ftok("ftok_pids", 17); 
if (pidKey == -1){ 
    perror("PID: Failed to load ftok file"); 
    return 1; 
} 

//-------------------------------------------------- 
//Shared Memory Initialization 
//-------------------------------------------------- 

//Initializing shared memory for clock counter 
clockID = shmget(clockKey, sizeof(int[2][1]), IPC_CREAT | 0666); 
if (clockID == -1){ 
    perror("Clock: Failed to designate shared memory"); 
    return 1; 
} 

clockVar = shmat(clockID, 0, 0); 
if (clockVar == (int*)-1){ 
    perror("Clock: Failed to attach shared memory"); 
    return 1; 
} 

//Initializing shared memory for the Message array 
msgID = shmget(msgKey, sizeof(int[2][1]), IPC_CREAT | 0666); 
if (msgID == -1){ 
    perror("Message: Failed to designate shared memory"); 
    return 1; 
} 

shmMsg = shmat(msgID, NULL, 0); 
if (shmMsg == (int*)-1){ 
    perror("Message: Failed to attach shared memory"); 
    return 1; 
} 

//Initializing shared memory for flag counter 
flagID = shmget(flagKey, sizeof(enum state[maxProc]), IPC_CREAT | 0666); 
if (flagID == -1){ 
    perror("Flag: Failed to designate shared memory"); 
    return 1; 
} 

flag = shmat(flagID, NULL, 0); 
if (flag == (enum state*)-1){ 
    perror("Flag: Failed to attach shared memory"); 
    return 1; 
} 

//Initializing shared memory for the process count array 
pidID = shmget(pidKey, sizeof(int), IPC_CREAT | 0666); 
if (pidID == -1){ 
    perror("PID: Failed to designate shared memory"); 
    return 1; 
} 

pidList = shmat(pidID, NULL, 0); 
if (pidList == (int*)-1){ 
    return 1; 
} 

編輯:作出建議編輯。標誌比較不會再拋出錯誤(枚舉狀態*)。現在我得到警告:不同指針類型的比較缺乏對其他三個(clockVar,shmMsg和pidList)的強制轉換。我還將我的聲明添加到頂部。我相信我只是搞亂了一些語法。謝謝!

+0

'shmget'返回一個'int' ..你的'* ID'變量是什麼類型? – yano

+0

我添加了上面的變量,並實現了(可能錯誤地)Felix建議的更改。添加了註釋。 – Arcie

回答

2

shmat()返回一個指針,並且您將它與一個整數進行比較。請參閱shmat() manpage

成功時shmat()返回附加共享內存段的地址;在錯誤(void *) -1返回

更改您這樣的代碼(假設你的變量聲明,你不要在你的問題顯示有正確的類型,在這種情況下,void *shmMsg;):

// [...] 
shmMsg = shmat(msgID, NULL, 0); 
if (shmMsg == (void*) -1){ 
    //[...] 

和你應該沒事。

+0

我編輯了上面的代碼,試圖實現這一點,它爲枚舉,但不是int指針數組。我究竟做錯了什麼? – Arcie