0

I don't understand why this code returns segmentation fault. Can anybody help me telling what's wrong?

#include <stdio.h>
#include "rk78.h"
#include "flux.h"
#include "rtbps.h"

#define N 6
#define h0 0.1
#define pasmin 1E-6
#define pasmax 10
#define tol 1E-13

int rk78 (double *t, double x[], double *h,
          double hmin, double hmax, double tol,
          int n, int (*camp)(int n, double t, double x[], double f[], void *prm),
          void *prm);

int flux (double *t, double x[], double *h, double T,
          double pasmin, double pasmax, double tol, int npasmx,
          int n,
          int (*camp)(int n, double t, double x[], double f[], void *prm),
          void *prm);

int rtbps (int n, double t, double *x, double *f, void *prm);
/*
  Per compilar:
  gcc -o rtbps_int -g -Wall rtbps_int.c rtbps.c flux.c rk78.c  -lm
  Executar:
  ./rtbps_int 1.215058560962404e-2 < halos_inp.txt > halos.txt
*/

int main (int argc, char *argv[]) {
    double t,x[N],h,mu,T;
    int i, nt;
    if (argc!=2
        || sscanf(argv[1],"%lf",&mu)!=1
        ) {
        fprintf(stderr, "./rtbps_int mu\n");
        return -1;
    }

    /*
      posició, velocitat, temps, iteracions
    */
    printf("Ok");
    while(scanf("%lf %lf %lf %lf %lf %lf %lf %d", 
                &x[0],&x[1], &x[2], &x[3],&x[4],&x[5],&T,&nt)==8){

        printf("%lf %lf %lf %lf %lf %lf %lf\n", t, 
               x[0],x[1],x[2],x[3],x[4],x[5]);
        h=h0;
        t=0;
        for(i=1;i<=nt;i++){
            //t=0;
            flux(&t,x,&h,T/nt,pasmin,pasmax,tol,nt,N,rtbps,&mu);
            printf("%lf %lf %lf %lf %lf %lf %lf\n", t, 
                   x[0],x[1],x[2],x[3],x[4],x[5]);
        }
    }
    return 0;
}

On the top I copied the prototype of the functions I use, but they work so they're not the problem.

Thank you!

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    What did you do after getting the seg fault? Any debugging at all? The correct thing to do is to use a debugger to debug your problem as much as you can before even considering asking others to debug for you. The debugger will tell you immediately which line is triggering the seg fault..and much more. – kaylum Feb 14 '17 at 22:33
  • You are using `&x[1]` etc., that does _not_ what you think. You need to use `&(x[1])`. Otherwise you get `(&x)[1]` which is undefined. – Aganju Feb 14 '17 at 23:17
  • @Aganju: `[]` has a higher precedence than `&`, so `&x[0]` does indeed evaluate to the address of `x[0]`. – Crowman Feb 14 '17 at 23:53
  • Thank you for your help!!! :D – Míriam Aumatell Feb 16 '17 at 08:39

1 Answers1

-1

In the line:

printf("%lf %lf %lf %lf %lf %lf %lf\n", t, 
    x[0],x[1],x[2],x[3],x[4],x[5]);

It doesn't appear as though t is initialized. Not sure if that is the cause but you can try initializing it and see if you get the same problem.

double t=0.0,x[N],h,mu,T;

If that's not the cause, then I would actually start suspecting the functions that you're trusting!

Yimin Rong
  • 1,890
  • 4
  • 31
  • 48