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.
// Oppenheimer-Snyder collapse
#include<iostream>
#include<cmath>
#include<fstream>
usingnamespace std;
// defining the size of the grids
constint J = 200;
constint T = 500;
int main()
{
// defining used constants
constdouble c = 29979245800; // speed of light
constdouble G = 6.67384e-8; // gravitational constant
constdouble pi = 3.14159;
constdouble R0 = sqrt(8/5); // initial radius
constdouble M = R0/5; // initial mass
constdouble M_gram = M*pow(c,2)/G; // mass of star in gram
constdouble chi0 = asin(sqrt(2*M/R0));
constdouble rho0 = M/((4*pi/3)*pow(R0,3)); // initial density
constdouble t_freefall_s = sqrt(3*pi/(32*G*M_gram/(4*pi*pow(R0,3)*(1/3))));
constdouble 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;
}
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 constint T = 400;