Program crashes while reading data from file into array

Dear all,

I'm trying to read information from .txt-files into arrays in C++ do use them for some numerics. I have eight two dimensional arrays and everything works fine, for the first two. But as soon as I try to read the data into a third array, the .exe created by my compiler crashes everytime with a message by Windows "Oppenheimer-Snyder.exe stopped working". I copied the statements for copying the data from the two working ones and used it to copy the data from the third file by simply replacing the filename and the array, which is why it is so puzzling to me, that it's just not working. The relevant part are the last three commented parts in main starting with the on commented with "initial data for U". I'm new to this forum and I don't know how to add the .txt files to my question and I'm not sure if I can just copy them, since each one has 201 floats stored in it and it will look pretty messy.

Thank you all for your help.

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
  // Oppenheimer-Snyder collapse

#include<iostream>
#include<cmath>
#include<fstream>

using namespace std;

// defining the size of the grids

const int J = 200;
const int T = 500;

int main()
{
    // defining used constants

    const double c = 29979245800; // speed of light
    const double G = 6.67384e-8; // gravitational constant
    const double pi = 3.14159;
    const double R0 = sqrt(8/5); // initial radius
    const double M = R0/5; // initial mass
    const double M_gram = M*pow(c,2)/G; // mass of star in gram
    const double chi0 = asin(sqrt(2*M/R0));
    const double rho0 = M/((4*pi/3)*pow(R0,3)); // initial density
    const double t_freefall_s = sqrt(3*pi/(32*G*M_gram/(4*pi*pow(R0,3)*(1/3))));
    const double t_freefall = c*t_freefall_s;

    // defining the used arrays
    double timesteps[T+1]; // holds half timesteps delta_u[n+1/2], first component is delta_u[n-1/2]
    double tau[J][T];
    double U[J][T+1]; // U[0] = U^(-1/2), U[1] = U^(1/2)
    double epsi[J][T+1]; // epsi[0] = epsi^(-1), epsi[1] = epsi^0
    double R[J][T];
    double m[J][T];
    double Gamma[J][T];
    double rho[J-1][T]; // rho[0] = rho_(1/2)

    // initial data

    // initial data for the timesteps: the first two timesteps are the freefall time
    timesteps[0] = t_freefall;
    timesteps[1] = t_freefall;

    // read the other initial data from the .txt files created with Maple
    int index; // index for the loops reading the initial data

    //initial data for U
    ifstream U_initial ("U_initial.txt");
    index = 0;
    while (index < J)
    {
        U_initial >> U[index][0];
        index++;
    }
    U_initial.close();

    // initial data for epsi
    ifstream epsi_initial ("epsi_initial.txt");
    index = 0;
    while (index < J)
    {
        epsi_initial >> epsi[index][0];
        index++;
    }
    epsi_initial.close();

    // initial data for R
    ifstream R_initial ("R_initial.txt");
    index = 0;
    while (index < J)
    {
        R_initial >> R[index][0];
        cout << R[index][0] << endl;
        index++;
    }




    return 0;
}
Last edited on
try to run it in debug mode.

Aceix.
When I'm running it in debug mode my compiler gives me the message: "Program received signal SIGSEGV, segmentation fault". It only happens when I add the part after // initial data for R. When i remove it, I don't get any such error message when using the debugger.
Ive just looked at your code and found that you a compiling very large arrays into your executable file,which i believe is causing this error... don't know how though.Try using dynamic memory allocation instead or use STL containers to store data.

I've managed to get this code to work by changing line 12 to
const int T = 400;
Last edited on
Thank you a lot for your answer. Once I used dynamic memory allocation it worked perfectly fine.
Topic archived. No new replies allowed.