# Getting Error -1073741819 (0xC0000005) - Using matrix after a specific number

0

So my task is to write a program that simulates covid-19 sickness chances. It's a University task, not a job.

I've tried to comment my code to be readable for anyone.

My main problem is, when I change the value of L to a number higher than 80 for example, I'll get error code: -1073741819 (0xC0000005).

The expected number by the teacher would be 150, so a matrix of 150*150 = 22500

Basically what the code does: It finds a random row and column = random person, and checks if he is sensitive to the virus (number=0) or if he is already sick (number=1)..

If he is sensitive, we generate a random number and we compare that to the number of Gamma, or Beta, depends if we want to heal him, or get him sick.

For my reviews on stackoverflow, I found out that I might have to make it a dynamic matrix, but I have no idea how to do that, since when I trited to change it to something like:

``````int *nepesseg= (int *)malloc(L*L* sizeof(int));
``````

I just got more errors, since I don't know how to refer to the matrix's elements anymore, etc.

I'm currently using Code::Blocks + GNU GCC Compiler.

``````#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
asked on Stack Overflow Oct 10, 2020 by Arany Sandor • edited Oct 10, 2020 by Arany Sandor

2

The exception `0xC0000005` is related to a memory violation on MS-WIN systems, which I think you're using.

About the code I don't see any formal error. Anyway it is not a good idea to allocate large array as automatic variables, move the declaration outside of `main` function, or prefix it with `static` modifier as in `static int population[L][L];`.

In any case it is really strange to trigger a memory exception with `L` up to 500 on my system with 1MB of allocated stack space.

The problem can come from the compiler you're using that allocates too small stack space.

answered on Stack Overflow Oct 10, 2020 by Frankie_C • edited Oct 10, 2020 by Frankie_C

User contributions licensed under CC BY-SA 3.0