Please help me: a problem on calling function in a loop

  1. Code (Text):

    for(k=0;k<10;k++)
        {
            c+=shift;
            dedispersion(c, displacement, fbk, result);
     
    Here, if I use "c=shift", the program runs well but the c will not change.
    If I keep the code like the above, then it can be compiled and linked but it will be closed when running. I am not able to change the parameter "c" in a loop.
    The whole code is followed:
    Code (Text):

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define PI 3.14159265358979323846264338327950288419716939937510
    #define TWO_PI (6.2831853071795864769252867665590057683943L)
    #define NF 33554432  //total number of data.
    #define NT 512   //number of channels.
    #define NTAO 32    // NTAO is the number that we use to creat filter bank.
    #define FS 14000100


    /* function prototypes */
    void checkN (int N);
    void dedispersion(double DM, int *displacement, double (*fbk)[NF/(NT*NTAO)], double (*result)[NF/(NT*NTAO)]);

    int
    main()
    {
        int i, j, k, *displacement;
        double DMbase, shift, (*fbk)[NF/(NT*NTAO)], (*result)[NF/(NT*NTAO)];
        double c=0.0;
        FILE *fp;
        char file[FILENAME_MAX];  // name of data file

        DMbase=50000000000000.0;
        shift=DMbase*0.1;

        /*initiation */
        checkN(NF);          // Check that NF = 2^n for some integer n >= 1.
        checkN(NT);
        if(!((fbk = malloc( NF/NTAO * sizeof(double)))&&
             (result = malloc( NF/NTAO * sizeof(double)))&&
             (displacement = malloc( NT * sizeof(int)))))
                printf("memory error \n");

       
       
        /*read data from txt file.*/

        if(!(fp = fopen("fbkwithdis.txt", "r")))
        {
            printf(" fbkwithdis.txt could not be opened!");
            exit(EXIT_FAILURE);
        }
        for(i=0;i<NT;i++)
        {
            for(j=0;j<NF/(NTAO*NT);j++)
                fscanf(fp, "%lf ", &fbk[i][j]);
            fprintf(fp, "\n");
        }
        fclose(fp);

        /*de-dispersion*/
        for(k=0;k<10;k++)
        {
            c+=shift;
            dedispersion(c, displacement, fbk, result);
               
            /*output data to txt file*/
           
            sprintf(file, "%d.txt", k);

            if(!(fp = fopen(file, "w")))
            {
                printf("   File \'%s\' could not be opened!", file);
                exit(EXIT_FAILURE);
            }
            for(i=0;i<NT;i++)
            {
                for(j=0;j<NF/(NTAO*NT);j++)
                    fprintf(fp, "%e ", result[i][j]);
                fprintf(fp, "\n");
            }
            fclose(fp);  
       
        }
       
    free(fbk);
    free(result);
    free(displacement);
    }


    /* Check that N = 2^n for some integer n >= 1. */
    void
    checkN (int N)
    {
        int i;

      if(N >= 2)
        {
          i = N;
          while(i==2*(i/2))
              i = i/2;  /* While i is even, factor out a 2. */
        }  /* For N >=2, we now have N = 2^n iff i = 1. */
      if(N < 2 || i != 1)
        {
          printf("NF, which does not equal 2^n for an integer n >= 1.");
          exit(EXIT_FAILURE);
        }
    }




    void dedispersion(double DM, int *displacement, double (*fbk)[NF/(NT*NTAO)], double (*result)[NF/(NT*NTAO)])
    {
        int i, j;
        int m=0, differ=0, k=0;    
        double time, frequency;

    /*find the displacement for each channel*/

        for(i=0;i<NT;i++)
        {  
       
        if(i<NT/2)
            frequency=(i*1.0)*FS/(NT-1);
        else
            frequency=(i*1.0)*FS/(NT-1)-FS;

        if(frequency-0.0<0.5&&frequency-0.0>-0.5)
            frequency=0.5;

        time=DM/((frequency*frequency)*TWO_PI);

        displacement[i]=(time/(1.0/FS))/(NT*NTAO);

        }


        for(i=0;i<NT;i++)
            if(displacement[i]>(NF/(NTAO*NT)))
                displacement[i]=NF/(NTAO*NT);
           
    /*  for(i=0;i<NT;i++)
            printf("%d, ", displacement[i]);
        printf("\n");
    */

        /*de-dispersion*/
        for(i=0;i<NT;i++)
        {
            for(j=0;j<NF/(NTAO*NT);j++)
            {
                result[i][j]=0.0;
                if((i!=0)&&(i<NT/2))
                    differ=displacement[i-1]-displacement[i];      
                else if((i!=NT-1)&&(i>NT/2-1))
                    differ=displacement[i+1]-displacement[i];
                else
                    differ=0;
       
       
                if(differ<0)
                    printf("error! differ is less than zero!\n");

                for(m=0;m<differ+1;m++)
                {
                    k=j+m+displacement[i];
                    if(k>NF/(NTAO*NT)-1)
                        break;
       
                    result[i][j]+=fbk[i][k];
               
                }
       
            }

        }

    }

     
     
  2. jcsd
  3. for update:
    I found that if I set a small value for NF, NT, then it works perfectly.
    for example,
    NF=8192
    NT=32
     
  4. rcgldr

    rcgldr 7,416
    Homework Helper

    Do you have access to a source level debugger that would let you step through the code and see how the variables are changing?

    Note that all of your defines are integers. Any math you do with those values will be done using integer math, unless you specifically cast them to (double) in mathematical expressions. I don't know if this is an issue for your program, as I didn't study it in detail.
     
  5. I don't think its the value of c not changing that is the problem

    Example with a simplified version of your code:
    Code (Text):

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>


    int
    main()
    {
      int k;
      double DMbase, shift;
      double c=0.0;
       
     
      DMbase=50000000000000.0;
      shift=DMbase*0.1;

      for(k=0;k<10;k++) {
        c+=shift;
        printf("Loop iter: %d Value of c: %0.2f\n", k, c);
      }

      return 0;
    }
     
    The output I get is:
    Loop iter: 0 Value of c: 5000000000000.00
    Loop iter: 1 Value of c: 10000000000000.00
    Loop iter: 2 Value of c: 15000000000000.00
    Loop iter: 3 Value of c: 20000000000000.00
    Loop iter: 4 Value of c: 25000000000000.00
    Loop iter: 5 Value of c: 30000000000000.00
    Loop iter: 6 Value of c: 35000000000000.00
    Loop iter: 7 Value of c: 40000000000000.00
    Loop iter: 8 Value of c: 45000000000000.00
    Loop iter: 9 Value of c: 50000000000000.00

    When you say closed when running what do you mean by this? Is it crashing in some form? A segmentation fault?
     
  6. yes,
    A segmentation fault.
    The c is not the problem.
    I try some constant instead of c, the segmentation fault appears also.

     
  7. Mark44

    Staff: Mentor

    You're not giving us enough information to help you with your problem. Your code is complex enough that you should be using a debugger to find where and why you're getting a segmentation fault.

    I suspect that the call to dedispersion is causing problems, for the reason that rcgldr gave in his earlier post.
     
  8. update:
    In linux system, the code works well. I can get the correct result.
    In windows system, it does not work sometimes.
    I can not find the reason. Now, I just use it because my work environment is linux.
     
Know someone interested in this topic? Share a link to this question via email, Google+, Twitter, or Facebook

Have something to add?