What causes Just-In-Time error?

Hello,
I'm quite new in C++ and I'm having a problem with a "Random walk in a 2 dimensional lattice with traps".

It doesn't appear to have any problems at the compiling, but when I'm trying to run the .exe an error message appears:
"An unhandled win32 exception occurred in *.exe. Just-In-Time debugging this exception failed with the following error: No installed debugger has Just-In-Time debugging enabled"

Is there anything wrong with my code that causes that error? Google didn't help me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <cstdlib>
#include <stdio.h>

#define RUNS 100000
#define L 1000
#define C1 0.01

using namespace std;

int main()
{
    int i,j,r,x,y,count;
  
    int **lattice; //this was int lattice[L][L]; fixed
    int *times;   //this was int times[RUNS]; fixed

    C=C1;
    //C=C2;
    srand(11111); //<- 2nd mistake was here. Fixed

    lattice=new int*[L];   //fixed

    times=new int[RUNS];  //fixed

    for (i=0;i<L;i++)
    {
        for (j=0;j<L;j++)
            lattice[i][j]=0;
    }

    count=int(C1*L*L);

    while (count>0)
    {
        r=rand()% 1000;
        x=r;
        r=rand()% 1000;
        y=r;
        if (lattice[x][y]!=1)
        {
            lattice[x][y]=1;
            count--;
        }
    }

    for (i=0;i<RUNS;i++)
    {
        r=rand()% 1000;
        x=r;
        r=rand()% 1000;
        y=r;

        while (lattice[x][y]==1)
        {
            r=rand()% 1000;
            x=r;
            r=rand()% 1000;
            y=r;
        }

        times[i]=0;

        while (lattice[x][y]==0)
        {
            r=rand()% 1000;

            if (r<250)
                x++;
            else if (r>=250 && r<500)
                x--;
            else if (r>=500 && r<750)
                y++;
            else
                y--;

            if (x==-1)
                x=999;
            if (x==1000)
                x=0;
            if (y==-1)
                y=999;
            if (y==1000)
                y=0;

            times[i]++;
        }
    }

    FILE *fp;
    fp=fopen("efarmoges5.txt","w");

    for (i=0;i<RUNS;i++)
        fprintf(fp,"%d \n",times[i]);

    fclose(fp);
}


Thank you in advance :)
Last edited on
What you are seeing is a fatal error in your program, but the OS can't find a debugger to help you find the error. One thing that is probably a problem: You have times defined as having 1000 entries; however you are looping over it 100,000 times!
omg I'm an idiot!
Thank you so much :)
Oh I just found another mistake too. I forgot to use srand :/
Zacaps, you're not an idiot! You just did something that all programmers to, every time they write code: They make typos! Stay with it: You'll get it!
Well it works now.
My crucial mistake was that I didn't define the large arrays dynamically.
There are more mistakes, the first three being in lines 5-7. You shouldn't abuse the preprocessor to define constants.
The correct version is:

1
2
3
const int runs=100000;
const int l=1000;
const double c1=0.01;


Those constants shouldn't be in global scope either if they're used in only one function.
There are more stylistical problems. In particular: don't declare variables before they're used, use fstreams instead fopen/printf, use vectors instead of allocating raw arrays (or when you do, delete[] them when you no longer need them) and here:
1
2
3
4
r=rand()% 1000;
x=r;
r=rand()% 1000;
y=r;

the intermediate assignments to r are unnecessary.


Remember that proper coding style avoids the vast majority of problems you would normally run into.
Topic archived. No new replies allowed.