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

  • Thread starter Thread starter nenyan
  • Start date Start date
  • Tags Tags
    Function Loop
Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to a loop in a C code that calls a function, specifically focusing on the behavior of a variable "c" and the occurrence of a segmentation fault during execution. Participants explore the implications of variable values, memory allocation, and system differences in code execution.

Discussion Character

  • Technical explanation
  • Debugging
  • Debate/contested

Main Points Raised

  • One participant notes that the program runs correctly when "c" is set to a constant value but fails when "c" is updated in a loop, leading to a segmentation fault.
  • Another participant suggests that the issue may not be with the variable "c" itself, providing a simplified code example that demonstrates the expected behavior of "c" in a loop.
  • Concerns are raised about the use of integer math due to the definition of constants, with a suggestion to cast values to double for calculations.
  • One participant mentions that the code works correctly on a Linux system but encounters issues on a Windows system, indicating potential platform-specific behavior.
  • A suggestion is made to use a source-level debugger to trace the code execution and identify the source of the segmentation fault.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the cause of the segmentation fault, with multiple competing views on whether the issue lies with the variable "c," the function call, or system-specific behavior.

Contextual Notes

Participants express uncertainty about the memory allocation and management in the code, particularly regarding the function "dedispersion" and its interaction with the variables. The discussion highlights the complexity of debugging in a multi-platform environment.

nenyan
Messages
67
Reaction score
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
for update:
I found that if I set a small value for NF, NT, then it works perfectly.
for example,
NF=8192
NT=32
 
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.
 
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?
 
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?
 
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.
 
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.
 

Similar threads

Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 6 ·
Replies
6
Views
12K
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K