Code Crashing - Possible Memory Management

I've written a rather hefty algorithm that requires storing very large arrays, and I believe it's making my code crash. I get the following errors:

"ViscousBurgers.exe has stopped working"
"Process returned -1073741571 <0xC00000FD> execution time : 38.529 s"

I've checked my code and I can't find anything that would cause it to crash like so, and when I run a coarser grid, the program seems to run fine. So my best guess is that this is a memory issue. I'm not sure how else to handle this as I have to store these two arrays. Do you guys have any suggestions as to how to overcome this?

I'm running Windows 7 64-bit with 16 gigs of ram, if that's of any help. Here is my code for reference:

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
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <fstream>
#include <cmath>

#define pi 3.14159265

using namespace std;

//Flow Paramters
double ax = 0;
double bx = 1;
double ay = 0;
double by = 1;
double at = 0;
double bt = 20;
double Nu = 0.015;
double W = 0.5*pi;

//Grid Spacing
const int L = 61;
const int M = 61;
double hx = (bx - ax)/(L - 1);
double hy = (by - ay)/(M - 1);
double dt = 0.001;
const int N = (bt - at)/dt + 1;

//Global Variables
double d = 0.5*dt/hx;
double f = 0.5*dt/hy;
double z = Nu*dt/hx/hx;
double p = Nu*dt/hy/hy;

int main()
{
    //Create X,Y,t Vectors
    double x [L];
    double y [M];
    double t [N];
    for (int i = 0; i < L; i++)
        x[i] = ax + i*hx;
    for (int j = 0; j < M; j++)
        y[j] = ay + j*hy;
    for (int k = 0; k < N; k++)
        t[k] = at + k*dt;

    //Boundary Conditions
    double U [L][M][N];
    double V [L][M][N];
    for (int k = 0; k < N; k++)
    {
        for (int j = 0; j < M; j++)
        {
            U[0][j][k] = -sin(W*t[k]);
            U[L-1][j][k] = 0;
            V[0][j][k] = 0;
            V[L-1][j][k] = 0;
        }
        for (int i = 0; i < L; i++)
        {
            U[i][0][k] = sin(2*pi*x[i]);
            U[i][M-1][k] = 1;
            V[i][0][k] = sin(W*t[k]);
            V[i][M-1][k] = 0;
        }
    }

    //Initial Conditions
    for (int j = 1; j < M-1; j++)
    {
        for (int i = 1; i < L-1; i++)
        {
            U[i][j][0] = 0;
            V[i][j][0] = 0;
        }
    }

    //FTCS Explicit Method
    for (int k = 1; k < N; k++)
    {
        for (int j = 1; j < M-1; j++)
        {
            for (int i = 1; i < L-1; i++)
            {
                U[i][j][k] = U[i][j][k-1] - d*(U[i+1][j][k-1]*U[i+1][j][k-1] - U[i-1][j][k-1]*U[i-1][j][k-1])
                    - f*(U[i][j+1][k-1]*V[i][j+1][k-1] - U[i][j-1][k-1]*V[i][j-1][k-1]) + z*(U[i+1][j][k-1]
                    - 2*U[i][j][k-1] + U[i-1][j][k-1]) + p*(U[i][j+1][k-1] - 2*U[i][j][k-1] + U[i][j-1][k-1]);
                V[i][j][k] = V[i][j][k-1] - d*(U[i+1][j][k-1]*V[i+1][j][k-1] - U[i-1][j][k-1]*V[i-1][j][k-1])
                    - f*(V[i][j+1][k-1]*V[i][j+1][k-1] - V[i][j-1][k-1]*V[i][j-1][k-1]) + z*(V[i+1][j][k-1]
                    - 2*V[i][j][k-1] + V[i-1][j][k-1]) + p*(V[i][j+1][k-1] - 2*V[i][j][k-1] + V[i][j-1][k-1]);
            }
        }
    }

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

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

    ofstream outputdata3("t.txt");
    for (int i = 0; i < N; i++)
    {
        outputdata3 << t[i] << endl;
    }
    outputdata3.close();

    ofstream outputdata4("Velocity.txt");
    for (int k = 0; k < N; k++)
    {
        for (int j = 0; j < M; j++)
        {
            for (int i = 0; i < L; i++)
            {
                outputdata4 << U[i][j][k] << "  " << V[i][j][k] << endl;
            }
        }
    }
    outputdata4.close();

    return 0;
}
Lines 40, 49 and 50 are not legal C++. N is not a true compile-time constant, so it can't be used to specify an array size. You're probably using a vla extension.

Have you tried moving the declarations for x, y, t, U and V outside the main() function so that they aren't allocated on the stack? It's possible the stack just isn't big enough to hold them.

So I did everything you recommended, and it seems to be working now. It seems that the biggest issue was having them allocated inside the main().

This brings up two questions for me,

1) why does having it outside main() make a difference? (sorry, I'm new to C++). It seemed to work just fine for smaller arrays.

2) Is there a better way to allocate/manage my memory?
Stuff defined in a function is allocated on the stack. Stuff defined at global or file scope is not (typically it resides in a data segment separate from the stack and heap.)

For large arrays that you don't want to be visible in so broad a scope using dynamically allocated memory obtained via new may be a good choice.
Topic archived. No new replies allowed.