-1
C에서 질병 시뮬레이터를 프로그래밍하려고합니다. while (1) 루프를 약 20-25 회 반복 한 후 몇 가지 이유로 인해 segfaults가 발생합니다. 그것은 완전히 무작위입니다. 나는이 문제를 몇 시간 동안 고치려고 노력했기 때문에 어떤 도움이라도 대단히 감사 할 것입니다.Segfault on 질병 시뮬레이터
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef struct space {
int type;
int x, y;
} space_t;
space_t space[40][40];
int main(){
bool infected = false;
int i = 0;
int x = 0;
int y = 0;
srand(time(NULL));
while(i < 1600){
space[x][y].x = x;
space[x][y].y = y;
if(rand() % 9 == 0 && !infected){
space[x][y].type = 1;
infected = true;
}
if(rand() % 20 == 8){
space[x][y].type = 2;
}
x++;
i++;
if(x == 40){
x = 0;
y++;
}
}
system("clear");
int count;
int inf = 0;
while(1){
x = 0;
y = 0;
i = 0;
while(i < 1600){
if(space[x][y].type == 1){
inf++;
}
if(space[x][y].type == 1 && rand() % 9 > 4){
if(rand() % 9 > 4){
space[x+(rand() % 3)][y].type = 1;
} else {
space[x+(-(rand() % 3))][y].type = 1;
}
} else if(space[x][y].type == 1 && rand() & 9 > 4){
if(rand() % 9 > 4){
space[x][y+(rand() % 3)].type = 1;
} else {
space[x][y+(-(rand() % 3))].type = 1;
}
}
if(space[x][y].type == 1){
printf("[I]");
} else if(space[x][y].type == 2){
printf("[D]");
} else printf("[ ]");
x++;
i++;
if(x == 40){
printf("\n");
x = 0;
y++;
}
}
count++;
printf("%d\n", count);
printf("%d\n", inf);
sleep(1);
system("clear");
}
return 0;
}
'&& rand() & 9> 4' -> &&'rand() % 9> 4'? 의심 스럽지만, 틀린 것 같습니다. – chux
인덱스가 범위를 벗어나지 않았는지 확인하십시오. –