I have this function called RRR for the class Table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void Table::RRR(constdouble *Yi) {
staticdouble eps;
/* code to calculate values for a local valarray fpi */
/* code to calculate eps (order of 1.0e-06) */
/* fpi and Yi are of the same size N */
/* the function W_Y() returns a double */
// compute new value of fpi
for (int i = 0; i < N; ++i) {
double dYi = (fpi[i] - Yi[i])/eps;
if(fabs(dYi)<1.0e-09) fpi[i] = 0.0;
else fpi[i] = W_Y()*dYidYc;
}
}
This code sometimes gives me segmentation fault on some architectures and runs fine on some others. If I just introduce a cout statment inside or right before the loop like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void Table::RRR(constdouble *Yi) {
staticdouble eps;
/* code to calculate values for a local valarray fpi */
/* code to calculate eps (order of 1.0e-06) */
/* fpi and Yi are of the same size N */
/* the function W_Y() returns a double */
// compute new value of fpi
for (int i = 0; i < N; ++i) {
cout << fpi[i]<<endl; /* I can output anything and it starts to work fine */
double dYi = (fpi[i] - Yi[i])/eps;
if(fabs(dYi)<1.0e-09) fpi[i] = 0.0;
else fpi[i] = W_Yc()*dYidYc;
}
}
Can anyone suggest me what is going wrong? I am pretty confident, the codes for calculating fpi and eps are fine. Infact, as soon as I introduce the cout statement, I get the result I want to get.
I am giving the function RRR in more detail.
RRR uses another function apply_fpi() to calculate the value of valarray fpi based on the arguements passed to RRR. Valarray fpi is a member of class Table. fpi is a valarray of size Nfpi where Nfpi = N+2, and N is the size of Yi_solver.
I have been using apply_fpi for another code for months now, and I am sure it calculates fpi of size Nfpi fine.
W_Yc() is a one line function which returns fpi[0]