Code Randomly Crashes

I have a code that is randomly crashing on me without any explanations. I'm not sure what is going on. When I run the code, it says that the .exe file has stopped working, and the command window displays the following:

"Process returned -1073741819 <0xC0000005> execution time : 16.747 s. Press any key to continue.

My compiler says the same thing without giving me any details.

I thought maybe I had an infinite while loop going on, but when I turn off my while loop, the code still crashes. I also tested out my function, SGN, to make sure that it works, and that doesn't seem to be the problem. The only thing that I can think of is the way I'm dynamically allocating my 2D arrays, U and E, and my 1D array, t. But on a previous thread, I thought I had this resolved, so I'm not totally sure.

I've attached my code for reference. I'm using CodeBlocks as my compiler. I really appreciate any help that I can get!

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <fstream>
#include <cmath>
#include <algorithm>

#define pi 3.14159265

using namespace std;

const int M = 40; //X-Grid Intervals
double L0 = -1;
double LF = 1;
double tmax = 0.15; //Final Time
int N = 40; //Maximum t-vector Size

//Dynamic Memory Allocation
double ** U;
double ** E;
double * t = new double [N];

int SGN(double aP)
{
    if (aP > 0)
    return 1;
    else if (aP == 0)
    return 0;
    else if (aP < 0)
    return -1;
}


int main()
{
    //Create X Vector and Initial Conditions
    double x [M+1];
    U = new double * [M+1];
    E = new double * [M+1];
    for ( int i = 0; i < M + 1; i++ ) * U = new double [N];
    for ( int i = 0; i < M + 1; i++ ) * E = new double [N];
    x[0] = L0;
    x[M] = LF;
    for (int i = 1; i < M; i++)
    {
        x[i] = -1 + (i - 0.5)*2/M;
        U[i][0] = 0.25 + 0.5*sin(pi*x[i]);
        E[i][0] = 0.5*U[i][0]*U[i][0];
    }
    U[0][0] = U[M-1][0];
    E[0][0] = 0.5*U[0][0]*U[0][0];
    U[M][0] = U[1][0];
    E[M][0] = 0.5*U[M][0]*U[M][0];
    double dx = x[2] - x[1];

    //Allocate Variables and Vectors
    double DUP;
    double DUM;
    double aP [M+1];
    double aM [M+1];
    double dtt [M+1];
    double dt;

    t[0] = 0;

    int s = 0;
    int ERR = 0;
    while (ERR == 0)
    {
        //Calculate Alpha and Time Step
        for (int i = 1; i < M; i++)
        {
            DUP = U[i+1][s] - U[i][s];
            DUM = U[i][s] - U[i-1][s];
            if (DUP == 0)
            {
                aP[i] = U[i][s];
            }
            else
            {
                aP[i] = (E[i+1][s] - E[i][s])/(U[i+1][s] - U[i][s]);
            }
            if (DUM == 0)
            {
                aM[i] = U[i][s];
            }
            else
            {
                aM[i] = (E[i][s] - E[i-1][s])/(U[i][s] - U[i-1][s]);
            }

            dtt[i] = dx/abs(aP[i]);

        }

        //Find Minimum Time Step and Round To Low Value
        dt = dtt[1];
        for (int i = 2; i < M; i++)
        {
            if (dtt[i] < dt)
            {
                dt = dtt[i];
            }
        }

        //Even Out Time Step
        if (t[s] + dt > tmax)
        {
            dt = tmax - t[s];
        }

        //Calculate Solution For New Time Step
        for (int i = 1; i < M; i++)
        {
            U[i][s+1] = U[i][s] - 0.5*dt/dx*(1 - SGN(aP[i]))*(E[i+1][s] - E[i][s]) - 0.5*dt/dx*(1 + SGN(aM[i]))*(E[i][s] - E[i-1][s]);
            E[i][s+1] = 0.5*U[i][s+1]*U[i][s+1];
        }
        //Period Boundary Condition
        U[0][s+1] = U[M-1][s+1];
        U[M][s+1] = U[1][s+1];
        E[0][s+1] = 0.5*U[0][s+1]*U[0][s+1];
        E[M][s+1] = 0.5*U[M][s+1]*U[M][s+1];

        //Calculate New Time
        t[s+1] = t[s] + dt;
        s = s + 1;

        //Expand Arrays
        if (s >= N)
        {
            N = N*2;
            double ** temp;
            double ** temp2;
            double * temp3 = new double [N];
            temp = new double * [M+1];
            temp2 = new double * [M+1];
            for ( int i = 0; i < M + 1; i++ ) * temp = new double [N];
            for ( int i = 0; i < M + 1; i++ ) * temp2 = new double [N];
            for (int j = 0; j < s; j++)
            {
                for (int i = 0; i < M+1; i++)
                {
                    temp[i][j] = U[i][j];
                    temp2[i][j] = E[i][j];
                }

                temp3[j] = t[j];
            }

            delete [] U;
            delete [] E;
            delete [] t;
            U = temp;
            E = temp2;
            t = temp3;

        }

        //Check To See If T-max Has Been Reached
        if (t[s] >= tmax)
        {
            ERR = 1;
        }

    }

    //Output Data Into Text File
    ofstream outputdata("timevector.txt");
    for (int i = 0; i<N; i++)
    {
        outputdata << t[i] << endl;
    }
    outputdata.close();

    ofstream outputdata3("xvector.txt");
    for (int j = 0; j<M+1; j++)
    {
        outputdata3 << x[j] << endl;
    }
    outputdata3.close();

    ofstream outputdata2("U.txt");
    for (int i = 0; i<N; i++)
    {
        for (int j = 0; j<M+1; j++)
        {
            outputdata2 << U[j][i] << endl;
        }
    }
    outputdata2.close();


    return 0;


}
To start with - I would re-check the following two lines
1
2
3
4
5
6
7
8
int main()
{
    //Create X Vector and Initial Conditions
    double x [M+1];
    U = new double * [M+1];
    E = new double * [M+1];
    for ( int i = 0; i < M + 1; i++ ) * U = new double [N]; //????????????????????
    for ( int i = 0; i < M + 1; i++ ) * E = new double [N]; //??????????????????????? 
Last edited on
I will admit, I'm not exactly sure what that line of code is doing. A couple of days ago, I had started a thread about dynamically allocating 2D arrays and that's what someone recommended to me. Here is the link to the thread:

http://www.cplusplus.com/forum/beginner/67879/

When I followed their advice, it seemed to get rid of all my old errors, but everything kept on crashing (as explained above). I must have misunderstood their suggestion or something.
the poster made a mistake:
You have a couple of choices (veyr simple fix):

Choice 1 - using array notation
1
2
    for ( int i = 0; i < M + 1; i++ )  U[i] = new double [N];
    for ( int i = 0; i < M + 1; i++ )  E[i] = new double [N];


OR

Choice 2 - using pointer notation
1
2
    for ( int i = 0; i < M + 1; i++ )  *(U+i) = new double [N];
    for ( int i = 0; i < M + 1; i++ )  *(E+i) = new double [N];



You also need to do the same later on when:
1
2
3
           for ( int i = 0; i < M + 1; i++ ) * temp = new double [N]; //do the same as above
            for ( int i = 0; i < M + 1; i++ ) * temp2 = new double [N];//do same as above
          


Other issues:

1. Your delete functions are wrong as well - your are leaking memory like a sieve.
Change to:

1
2
3
4
5
            for ( int i = 0; i < M + 1; i++ )  delete [] U[i] ;
            for ( int i = 0; i < M + 1; i++ )  delete [] E[i] ;       
            delete [] U;
            delete [] E;
            delete [] t;


2. This function is incomplete - as it is possible to exit the function
without providing a value (your compiler should have warned you as such
1
2
3
4
5
6
7
8
9
int SGN(double aP)
{
    if (aP > 0)
    return 1;
    else if (aP == 0)
    return 0;
    else if (aP < 0)
    return -1;
}


change it to:
1
2
3
4
5
6
7
8
9
10
11
int SGN(double aP)
{
    if (aP > 0)
    return 1;
    else if (aP == 0)
    return 0;
   //you have already accounted for ap>0 and ap==0
//so if we get here ap must be greater than 0
//so just return -1 
    return -1;
}



That's all for now... once you have done those we can go on from there.
Last edited on
I had troubles with an array such as this. A little background, the file I was reading had an integer rep which would be the amount of sections. In each section was a character array, the 4 bytes before it being an integer for how long the array would be.

I idea was, make the first dimension from the first rep, and then have varied sizes of the 2nd dimension. I guess we can't do this with arrays. I settled to have the 2nd dimension be something larger than it would ever need to be, but I would imagine your global variable would work the same (Which begs the question, why even make them dynamically if you know what size they are going to be?).

Here is the snipet of code, the parenthesis are important:

1
2
unsigned char (*u53String)[64];
u53String = new unsigned char [u53Rep][64];


I think if they were both dynamic, you would want something like this: *(*variable);
Last edited on
the poster made a mistake:
You have a couple of choices (veyr simple fix):

Choice 1 - using array notation
1
2
for ( int i = 0; i < M + 1; i++ )  U[i] = new double [N];
for ( int i = 0; i < M + 1; i++ )  E[i] = new double [N];



You're the man! Things are working out perfectly. Is there any way you can explain to me how the language is handling this in order to dynamically allocate the memory? It seems to me that I'm creating a vector (1D) and then through the for-loop, I'm adding another vector of size N to each element of my first vector. It's just the way that it's coded makes no sense to me.

Other issues:

1. Your delete functions are wrong as well - your are leaking memory like a sieve. We'll fix that later..


What's the best way to go about this? I was always told that in C++ you want to get rid of those unnecessary vectors and the best way to do them was to just use 'delete'.
Err, but you have to make sure you delete[] everything.

That would mean (I haven't really looked at your code; sorry if this is wrong)
1
2
3
4
5
6
7
for (int i = 0; i < M + 1; ++i)
{
    delete[] U[i];
    delete[] E[i];
}
delete[] U;
delete[] E;
2. This function is incomplete - as it is possible to exit the function
without providing a value (your compiler should have warned you as such


Although I agree that the way you corrected makes more sense, there isn't a case where that function wouldn't return anything.
Last edited on
Err, but you have to make sure you delete[] everything.

That would mean (I haven't really looked at your code; sorry if this is wrong)
1
2
3
4
5
6
7
for (int i = 0; i < M + 1; ++i)
{
    delete[] U[i];
    delete[] E[i];
}
delete[] U;
delete[] E;


Do you have to do the for-loop because it's a 2D array? I was always under the impression that 'delete []' would simply erase everything.

Also, another thing I noticed is that my code runs fine unless it has to utilize the following section (expanding arrays). This section seems to crash my program every time. Is it perhaps the way I'm allocating my arrays? I'm using the same method as above, so it shouldn't be doing that. Or is it because I have to run a for-loop when equation U = temp, E = temp2, t = temp3?

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
//Expand Arrays
        if (s >= N)
        {
            N = N*2;
            double ** temp;
            double ** temp2;
            double * temp3 = new double [N];
            temp = new double * [M+1];
            temp2 = new double * [M+1];
            for ( int i = 0; i < M + 1; i++ ) temp[i] = new double [N];
            for ( int i = 0; i < M + 1; i++ ) temp2[i] = new double [N];
            for (int j = 0; j < s; j++)
            {
                for (int i = 0; i < M+1; i++)
                {
                    temp[i][j] = U[i][j];
                    temp2[i][j] = E[i][j];
                }

                temp3[j] = t[j];
            }

            for (int i = 0; i < M+1; i++)
            {
                delete [] U[i];
                delete [] E[i];
            }

            delete [] U;
            delete [] E;
            delete [] t;
            U = temp;
            E = temp2;
            t = temp3;

        }
Last edited on
Topic archived. No new replies allowed.