出现错误 -1073741819 (0xC0000005) - 在特定数字后使用矩阵
Getting Error -1073741819 (0xC0000005) - Using matrix after a specific number
所以我的任务是编写一个模拟 covid-19 患病几率的程序。这是大学的任务,不是工作。
我尝试对我的代码进行注释,以便任何人都可以阅读。
我的主要问题是,当我将 L 的值更改为大于 80 的数字时,我会得到错误代码:-1073741819 (0xC0000005)。
老师的期望数是150,所以矩阵150*150 = 22500
基本上代码的作用是:
它找到一个随机的行和列=随机人,并检查他是否对病毒敏感(数字=0)或者他是否已经生病(数字=1)..
如果他很敏感,我们会生成一个随机数,然后将其与 Gamma 或 Beta 的数量进行比较,这取决于我们是要治愈他还是让他生病。
对于我对 Whosebug 的评论,我发现我可能必须将其设为动态矩阵,但我不知道该怎么做,因为当我尝试将其更改为类似以下内容时:
int *nepesseg= (int *)malloc(L*L* sizeof(int));
我只是遇到了更多错误,因为我不知道如何再引用矩阵的元素等等。
我目前正在使用 Code::Blocks + GNU GCC 编译器。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#define L 70 // IF THIS NUMBER IS MORE, LIKE 100, I GET "-1073741819 (0xC0000005)" Error code
int main()
{
srand(time(NULL));
int day=365, randomPersonRow=0,randomPersonColumn=0;
double beta=0.7,gamma=0.1,odds=0.0; //Gamma = healing chance, Beta = chance for getting sick
int SensitiveCount,SickCount,HealedCount;
int population[L][L];
SensitiveCount=(L*L)-1;SickCount=1;HealedCount=0;
int i,j,v,u;
for(i=0; i<L; i++) {
for(j=0;j<L;j++) {
population[i][j]=0;
}
}
randomPersonRow=(rand()%L);
randomPersonColumn=(rand()%L);
population[randomPersonRow][randomPersonColumn]=1; //first sick person - generated random
FILE *fp;
fp = fopen("result.dat", "w");
for(v=1; v<=day; v++){ //365 days
for(u=0;u<=L*L;u++){ //1 day
randomPersonRow=(rand()%L); //Find a random person to see if he is sick or not
randomPersonColumn=(rand()%L); //Find a random person to see if he is sick or not
//for(i=0; i<L; i++) {
//for(j=0;j<L;j++) {
if(population[randomPersonRow][randomPersonColumn]==0){ //0 means he is Sensitive, and not sick or healed yet
if(population[randomPersonRow+1][randomPersonColumn]==1){ // If he meets a sick person in his "matrix" - 4 chance(up, down, left, right)
odds=((double) rand() / (RAND_MAX));
if(odds<=beta){
population[randomPersonRow][randomPersonColumn]=1; // He is getting sick, if he has been in contact with a sick person
SensitiveCount=SensitiveCount-1;SickCount=SickCount+1;
}
}
else if(population[randomPersonRow][randomPersonColumn+1]==1){
odds=((double) rand() / (RAND_MAX));
if(odds<=beta){
population[randomPersonRow][randomPersonColumn]=1; // He is getting sick, if he has been in contact with a sick person
SensitiveCount=SensitiveCount-1;SickCount=SickCount+1;
}
}
else if(population[randomPersonRow-1][randomPersonColumn]==1){
odds=((double) rand() / (RAND_MAX));
if(odds<=beta){
population[randomPersonRow][randomPersonColumn]=1; // He is getting sick, if he has been in contact with a sick person
SensitiveCount=SensitiveCount-1;SickCount=SickCount+1;
}
}
else if(population[randomPersonRow][randomPersonColumn-1]==1){
odds=((double) rand() / (RAND_MAX));
if(odds<=beta){
population[randomPersonRow][randomPersonColumn]=1; // He is getting sick, if he has been in contact with a sick person
SensitiveCount=SensitiveCount-1;SickCount=SickCount+1;
}
}
}
if(population[randomPersonRow][randomPersonColumn]==1){ // 1 = If the random person is sick
odds=((double) rand() / (RAND_MAX));
if(odds<=gamma){
population[randomPersonRow][randomPersonColumn]=2; // He is healed now, if the odds are less then the random gamma value.
SickCount=SickCount-1;HealedCount=HealedCount+1;
}
}
//}
//}
}
if(fp!=NULL){
fprintf(fp,"%d; %d; %d; %d; \n",v,SensitiveCount,SickCount,HealedCount); // writing to file
}else{
printf("Error!");
}
}
fclose(fp);
return 0;
}
异常 0xC0000005
与 MS-WIN 系统上的内存冲突有关,我认为您正在使用该系统。
关于代码,我没有看到任何形式上的错误。无论如何,将大数组分配为自动变量、将声明移到 main
函数之外,或在其前面加上 static
修饰符都不是一个好主意,如 static int population[L][L];
.
无论如何,在我的系统上使用 1MB 分配的堆栈 space.
触发内存异常 L
高达 500 真的很奇怪
问题可能出在您使用的编译器分配的堆栈太小 space。
所以我的任务是编写一个模拟 covid-19 患病几率的程序。这是大学的任务,不是工作。
我尝试对我的代码进行注释,以便任何人都可以阅读。
我的主要问题是,当我将 L 的值更改为大于 80 的数字时,我会得到错误代码:-1073741819 (0xC0000005)。
老师的期望数是150,所以矩阵150*150 = 22500
基本上代码的作用是: 它找到一个随机的行和列=随机人,并检查他是否对病毒敏感(数字=0)或者他是否已经生病(数字=1)..
如果他很敏感,我们会生成一个随机数,然后将其与 Gamma 或 Beta 的数量进行比较,这取决于我们是要治愈他还是让他生病。
对于我对 Whosebug 的评论,我发现我可能必须将其设为动态矩阵,但我不知道该怎么做,因为当我尝试将其更改为类似以下内容时:
int *nepesseg= (int *)malloc(L*L* sizeof(int));
我只是遇到了更多错误,因为我不知道如何再引用矩阵的元素等等。
我目前正在使用 Code::Blocks + GNU GCC 编译器。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#define L 70 // IF THIS NUMBER IS MORE, LIKE 100, I GET "-1073741819 (0xC0000005)" Error code
int main()
{
srand(time(NULL));
int day=365, randomPersonRow=0,randomPersonColumn=0;
double beta=0.7,gamma=0.1,odds=0.0; //Gamma = healing chance, Beta = chance for getting sick
int SensitiveCount,SickCount,HealedCount;
int population[L][L];
SensitiveCount=(L*L)-1;SickCount=1;HealedCount=0;
int i,j,v,u;
for(i=0; i<L; i++) {
for(j=0;j<L;j++) {
population[i][j]=0;
}
}
randomPersonRow=(rand()%L);
randomPersonColumn=(rand()%L);
population[randomPersonRow][randomPersonColumn]=1; //first sick person - generated random
FILE *fp;
fp = fopen("result.dat", "w");
for(v=1; v<=day; v++){ //365 days
for(u=0;u<=L*L;u++){ //1 day
randomPersonRow=(rand()%L); //Find a random person to see if he is sick or not
randomPersonColumn=(rand()%L); //Find a random person to see if he is sick or not
//for(i=0; i<L; i++) {
//for(j=0;j<L;j++) {
if(population[randomPersonRow][randomPersonColumn]==0){ //0 means he is Sensitive, and not sick or healed yet
if(population[randomPersonRow+1][randomPersonColumn]==1){ // If he meets a sick person in his "matrix" - 4 chance(up, down, left, right)
odds=((double) rand() / (RAND_MAX));
if(odds<=beta){
population[randomPersonRow][randomPersonColumn]=1; // He is getting sick, if he has been in contact with a sick person
SensitiveCount=SensitiveCount-1;SickCount=SickCount+1;
}
}
else if(population[randomPersonRow][randomPersonColumn+1]==1){
odds=((double) rand() / (RAND_MAX));
if(odds<=beta){
population[randomPersonRow][randomPersonColumn]=1; // He is getting sick, if he has been in contact with a sick person
SensitiveCount=SensitiveCount-1;SickCount=SickCount+1;
}
}
else if(population[randomPersonRow-1][randomPersonColumn]==1){
odds=((double) rand() / (RAND_MAX));
if(odds<=beta){
population[randomPersonRow][randomPersonColumn]=1; // He is getting sick, if he has been in contact with a sick person
SensitiveCount=SensitiveCount-1;SickCount=SickCount+1;
}
}
else if(population[randomPersonRow][randomPersonColumn-1]==1){
odds=((double) rand() / (RAND_MAX));
if(odds<=beta){
population[randomPersonRow][randomPersonColumn]=1; // He is getting sick, if he has been in contact with a sick person
SensitiveCount=SensitiveCount-1;SickCount=SickCount+1;
}
}
}
if(population[randomPersonRow][randomPersonColumn]==1){ // 1 = If the random person is sick
odds=((double) rand() / (RAND_MAX));
if(odds<=gamma){
population[randomPersonRow][randomPersonColumn]=2; // He is healed now, if the odds are less then the random gamma value.
SickCount=SickCount-1;HealedCount=HealedCount+1;
}
}
//}
//}
}
if(fp!=NULL){
fprintf(fp,"%d; %d; %d; %d; \n",v,SensitiveCount,SickCount,HealedCount); // writing to file
}else{
printf("Error!");
}
}
fclose(fp);
return 0;
}
异常 0xC0000005
与 MS-WIN 系统上的内存冲突有关,我认为您正在使用该系统。
关于代码,我没有看到任何形式上的错误。无论如何,将大数组分配为自动变量、将声明移到 main
函数之外,或在其前面加上 static
修饰符都不是一个好主意,如 static int population[L][L];
.
无论如何,在我的系统上使用 1MB 分配的堆栈 space.
触发内存异常L
高达 500 真的很奇怪
问题可能出在您使用的编译器分配的堆栈太小 space。