【问题标题】:Getting Error -1073741819 (0xC0000005) - Using matrix after a specific number出现错误 -1073741819 (0xC0000005) - 在特定数字后使用矩阵
【发布时间】:2021-01-25 08:58:08
【问题描述】:

所以我的任务是编写一个模拟 covid-19 患病几率的程序。这是大学的任务,而不是工作。

我已尝试将我的代码注释为任何人都可以阅读。

我的主要问题是,例如,当我将 L 的值更改为大于 80 的数字时,我会得到错误代码:-1073741819 (0xC0000005)。

老师期望的数字是 150,所以矩阵是 150*150 = 22500

代码的基本作用: 它找到一个随机的行和列=随机的人,并检查他是否对病毒敏感(数字=0)或者他是否已经生病(数字=1)..

如果他很敏感,我们会生成一个随机数,然后将其与 Gamma 或 Beta 的数量进行比较,这取决于我们是想治愈他还是让他生病。

在我对 stackoverflow 的评论中,我发现我可能必须将其设为动态矩阵,但我不知道该怎么做,因为当我尝试将其更改为以下内容时:

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;
}


【问题讨论】:

    标签: c windows matrix malloc stack-overflow


    【解决方案1】:

    0xC0000005 异常与我认为您正在使用的 MS-WIN 系统上的内存冲突有关。

    关于代码,我没有看到任何形式上的错误。无论如何,将大数组分配为自动变量,将声明移到main 函数之外,或者在static int population[L][L]; 中使用static 修饰符作为前缀,这不是一个好主意。

    无论如何,在我的系统上使用 1MB 分配的堆栈空间触发 L 高达 500 的内存异常真的很奇怪。

    问题可能来自您使用的编译器分配的堆栈空间太小。

    【讨论】:

    • 数组大小为 4 * 500 * 500 ,堆栈很大
    • @pm100 我也有同样的想法。我在我的系统上做了一个测试,在崩溃之前最多可以分配 500 个元素,1MB 堆栈。但是 150 个元素应该没问题。他使用的是哪个编译器?
    • 我正在使用 GNU GCC 编译器
    • @AranySandor 使用static 限定符是否有效?总之还是很奇怪。即使编译为 64 位长的 int,您也应该可以管理 150*150*8=180kB,这对于标准的 1MB 堆栈分配来说不是什么大问题...
    • 是的,当我使用静态限定符时它确实有效,所以谢谢你,它解决了我的问题,但这确实很奇怪。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多