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

In summary, the code provided has some issues with running on Windows systems, but runs well on Linux systems. The main issue may be related to a segmentation fault, which can be better diagnosed using a debugger. The code should be reviewed and potentially rewritten to ensure compatibility with both operating systems.
  • #1
nenyan
67
0
Code:
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:
#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];
			
			}
	
		}

	}

}
 
Technology news on Phys.org
  • #2
for update:
I found that if I set a small value for NF, NT, then it works perfectly.
for example,
NF=8192
NT=32
 
  • #3
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.
 
  • #4
I don't think its the value of c not changing that is the problem

Example with a simplified version of your code:
Code:
#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?
 
  • #5
yes,
A segmentation fault.
The c is not the problem.
I try some constant instead of c, the segmentation fault appears also.

gbeagle said:
I don't think its the value of c not changing that is the problem

Example with a simplified version of your code:
Code:
#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
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.
 
  • #7
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.
 

1. What is the common issue when calling a function in a loop?

The most common issue when calling a function in a loop is that the loop may execute faster than the function, causing it to be called multiple times with the same input before it has a chance to complete. This can lead to unexpected results and errors.

2. How can this issue be solved?

To solve this issue, you can use a setTimeout() function to delay the execution of the loop. This will give the function enough time to complete before the next iteration of the loop begins.

3. Can this issue also occur with asynchronous functions?

Yes, this issue can also occur with asynchronous functions. In this case, you can use async/await or Promise.all() to ensure that the loop waits for the asynchronous function to complete before moving on to the next iteration.

4. Are there any other potential problems when calling a function in a loop?

Another potential problem is that the function may modify a variable that is used in the loop condition, causing an infinite loop. To avoid this, make sure to use let or var instead of const for the loop variable.

5. Can recursive functions also cause issues when called in a loop?

Yes, recursive functions can also cause issues when called in a loop. This is because each recursion adds a new call to the function on top of the previous ones, potentially leading to a stack overflow. To prevent this, make sure to have a base case that will end the recursion.

Similar threads

  • Programming and Computer Science
Replies
1
Views
940
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
4
Views
602
  • Programming and Computer Science
Replies
5
Views
3K
Replies
3
Views
3K
Replies
1
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
3K
Back
Top