Segmentation fault

Hi,

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(const double *Yi) {
         static double 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(const double *Yi) {
         static double 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.

thanks.
As always, does Yi and fpi have at least N elements?
Yes I agree. I would be suspicious of the contents of N.

Make sure your arrays always contains *at least* N elements.
any suggestions on how do I check that?
as soon as I cout something, the code starts working fine.
You haven't provided the declaration of any of these so we can't really help any further.
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]

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
void Table::RRR(const double& Yc,
                                const double& Ycv,
                                const double& z_star,
                                const double& zv,
                                const double& Tu_star,
                                const double *Yi) {

  static double Yc_perturbed;
 
   // update Wyc
    apply_fpi(Yc, Ycv, z_star, zv, Tu_star);
 
    const double abs_tol( 1.0e-5 ); // absolute tolerance
    const double rel_tol( 1.0e-3 ); // relative tolerance
    static  double eps;

    // perturb Yc
    eps = abs_tol + Yc * rel_tol;
    Yc_perturbed = Yc + eps;

    // get perturbed fpi
    apply_fpi(Yc_perturbed, Ycv, z_star, zv, Tu_star);

    // compute new fpi
    for (int i = 2; i < Nfpi; ++i) {
      double dYi = (fpi[i] - Yi[i-2])/eps;
      if(fabs(dYi)<1.0e-09) fpi[i] = 0.0;
      else fpi[i] = W_Yc()*dYi;     
    }
  }
  


does this information help regarding how should I check the size of both arrays?
No. We need to see the declarations of Yi, fpi and the value of N.
Topic archived. No new replies allowed.