2017-10-10 1 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; 
} 

편집 : 제안 된 편집. 플래그 비교는 더 이상 오류를 throw하지 않습니다 (열거 형 상태 *). 이제 경고가 발생합니다 : 다른 포인터 유형의 비교가 다른 세 가지 (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

위 코드를 구현하기 위해 위의 코드를 편집했지만 enum에는 작동했지만 int 포인터 배열에는 작동하지 않았습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? – Arcie

관련 문제