by nenyan
Tags: calling, function, loop
 P: 56 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: #include #include #include #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= 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-0.5) frequency=0.5; time=DM/((frequency*frequency)*TWO_PI); displacement[i]=(time/(1.0/FS))/(NT*NTAO); } for(i=0;i(NF/(NTAO*NT))) displacement[i]=NF/(NTAO*NT); /* for(i=0;iNT/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;mNF/(NTAO*NT)-1) break; result[i][j]+=fbk[i][k]; } } } }
 P: 56 for update: I found that if I set a small value for NF, NT, then it works perfectly. for example, NF=8192 NT=32
 HW Helper P: 7,134 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.
 P: 53 Please help me: a problem on calling function in a loop I don't think its the value of c not changing that is the problem Example with a simplified version of your code: #include #include #include 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?
P: 56
yes,
A segmentation fault.
The c is not the problem.
I try some constant instead of c, the segmentation fault appears also.

 Quote by gbeagle I don't think its the value of c not changing that is the problem Example with a simplified version of your code: #include #include #include 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?
 Mentor P: 21,313 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.
 P: 56 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.

 Related Discussions Engineering, Comp Sci, & Technology Homework 4 Programming & Computer Science 2 Programming & Computer Science 7 Math & Science Software 2 Engineering, Comp Sci, & Technology Homework 2