1,105,310 Community Members

Strange error in a small code

Member Avatar
johnnydarten
Newbie Poster
21 posts since May 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Dear all,

I'm a C beginner and I was doing a small practice nested loop on C when I got a very strange error which when the console opens tells me that my program has stopped working.

here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>	
    
main()
{	int i,j,k;
	int i_range, j_range, k_range; 

	i_range = 800;
	j_range = 20-1;
	k_range = 1000-1; 
	float tt=0;
	float tendrecord=1000;
	float Hy_yplane_measure_cos[800][999][1];
	float Hy_yplane_measure_sin[800][999][1];
	int a_n=500;
	float light_speed=3e8;
	float S=2;
	int lattice_x=100;
	float freqmeasure=30;
	
j=floor(0.5+((0.1)*100));
if(tt<tendrecord)
{
 for (k=k_range;k>=1;k--)
  for(i=1;i<=i_range;i++)	
{
 Hy_yplane_measure_cos[i][k][1]+=1*cos(2*3.1416*freqmeasure*tt*(a_n*1E9)/(light_speed*S*lattice_x));
                 }    
}
}

I'm using Dev C++ compiler with a C project(Console) and I have windows 7 on my laptop.
can someone help me?

Member Avatar
Ancient Dragon
Achieved Level 70
27,572 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

1. Move those two huge arrays on lines 15 and 16 up above main() so that they will not be on the limited stack.


2. All array idices are numbered 0 to, but not including, the declared array size. So an array with 800 elements are numbered 0 to 799.

The two loops on lines 26 and 27 exceed the limits of those two arrays by 1 element each. You need to modify them as shown below.

for (k=k_range-1;k>=0;k--)
  for(i=0;i<i_range;i++)

3. There is no point in declaring those two arrays on lines 15 and 16 as 3dimensional arrays -- 2-dimensional arrays is sufficient for your purposes

float Hy_yplane_measure_cos[800][999];
float Hy_yplane_measure_sin[800][999];

4. change line 29 like this (its wrong as you posed it): Hy_yplane_measure_cos[i][k]+=1*cos(2*3.1416*freqmeasure*tt*(a_n*1E9)/(light_speed*S*lattice_x))

Member Avatar
johnnydarten
Newbie Poster
21 posts since May 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks ancient dragon for your response.

Actually this doesn't solve my real problem. The problem is shown in the code attached. This is the real problem without any simplification. So if you could know the problem or how to fix it I will be thankful. I hope the code is simple.

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

#define pi 3.141592

void out_plane(char *component,char *plane,float value,char *lastname,long tt);
float grid_value(char *component,int i,int j,int k);
float ***float_3d_memory(int imax,int jmax,int kmax);
float **float_2d_memory(int imax,int jmax);
float efieldx(int i,int j,int k);
float efieldy(int i,int j,int k);
float efieldz(int i,int j,int k);
float hfieldx(int i,int j,int k);
float hfieldy(int i,int j,int k);
float hfieldz(int i,int j,int k);


int use_periodic_x=1;
int use_periodic_y=1;
int isize=100*8;
int jsize=100*0.2;
int ksize=100*10; 
float xcenter=4;
float ycenter=0.1;
float zcenter=5;
float lattice_x=100;
float lattice_y=100;
float lattice_z=100;

float a_n=500;
float lambdameasure=650E-9;
double	eo=8.854e-12;
double	uo=pi*4.0e-7;
float light_speed;
float freqmeasure;
float S_factor=2.0;
float ds_x=1.0;
float tendrecord;

float ***Ex_yplane_measure_cos, ***Ex_yplane_measure_sin;
float ***Ez_yplane_measure_cos, ***Ez_yplane_measure_sin;
float ***Hy_yplane_measure_cos, ***Hy_yplane_measure_sin;
float ***Ex,***Ey,***Ez;
float ***Hx,***Hy,***Hz; 


 
main()
{long t;
 long DD=16000;
 float out_plane_y=0;
 
 Ex_yplane_measure_cos=float_3d_memory(isize,ksize);
 Ex_yplane_measure_sin=float_3d_memory(isize,ksize);
 Ez_yplane_measure_cos=float_3d_memory(isize,ksize);
 Ez_yplane_measure_sin=float_3d_memory(isize,ksize);
 Hy_yplane_measure_cos=float_3d_memory(isize,ksize);
 Hy_yplane_measure_sin=float_3d_memory(isize,ksize);
 Ex=float_3d_memory(isize,jsize,ksize);
 Ey=float_3d_memory(isize,jsize,ksize);
 Ez=float_3d_memory(isize,jsize,ksize);
 Hx=float_3d_memory(isize,jsize,ksize);
 Hy=float_3d_memory(isize,jsize,ksize);
 Hz=float_3d_memory(isize,jsize,ksize);
    
 light_speed=1.0/sqrt(eo*uo);
 freqmeasure=light_speed/lambdameasure;
 tendrecord=DD;
 
 	for(t=0;t<=DD;t++)                        
	{out_plane("Hy","y",out_plane_y,".Hy",t);
	 printf("%d\n",t);}	
}

void out_plane(char *component,char *plane,float value,char *lastname,long tt)
{
	FILE *file;
	char name[20];
	int i,j,k;
	int i_range, j_range, k_range; 
	// for Hz_parity, normal cases
	i_range = isize-1;
	j_range = jsize-1;
	k_range = ksize-1; 
	
	if(use_periodic_x == 1)
		i_range = isize;
	if(use_periodic_y == 1)
		j_range = jsize; 
	
	sprintf(name,"%07d",tt);
	strcat(name,lastname);
	file=fopen(name,"w");

	if(strcmp("x",plane)==0)
	{
		i=floor(0.5+((value+xcenter)*lattice_x));
		for(k=k_range;k>=1;k--)
		{
				for(j=1;j<=j_range;j++)	fprintf(file,"%g ",grid_value(component,i,j,k));
				fprintf(file,"\n");
		}
	}

	if(strcmp("y",plane)==0)
	{
		j=floor(0.5+((value+ycenter)*lattice_y));
if(tt<tendrecord)
{
 for (k=k_range;k>=1;k--)
 {
				for(i=1;i<=i_range;i++)	
				{if(strcmp(component,"Ex")==0)
                 {Ex_yplane_measure_cos[i][k][1]+=grid_value(component,i,j,k)*cos(2*pi*freqmeasure*tt*(a_n*1E-9)/(light_speed*S_factor*ds_x*lattice_x));
                 Ex_yplane_measure_sin[i][k][1]+=grid_value(component,i,j,k)*sin(2*pi*freqmeasure*tt*(a_n*1E-9)/(light_speed*S_factor*ds_x*lattice_x));
                 }
                 else if(strcmp(component,"Ez")==0)
				 {Ez_yplane_measure_cos[i][k][1]+=grid_value(component,i,j,k)*cos(2*pi*freqmeasure*tt*(a_n*1E-9)/(light_speed*S_factor*ds_x*lattice_x));
				 Ez_yplane_measure_sin[i][k][1]+=grid_value(component,i,j,k)*sin(2*pi*freqmeasure*tt*(a_n*1E-9)/(light_speed*S_factor*ds_x*lattice_x));
                 }
				 else if(strcmp(component,"Hy")==0)
				 {//////IF ERROR IN RESULT LOOK AT FFT OF FARFIELD.C
                 Hy_yplane_measure_cos[i][k][1]+=grid_value(component,i,j,k)*cos(2*pi*freqmeasure*tt*(a_n*1E-9)/(light_speed*S_factor*ds_x*lattice_x));
                 Hy_yplane_measure_sin[i][k][1]+=grid_value(component,i,j,k)*sin(2*pi*freqmeasure*tt*(a_n*1E-9)/(light_speed*S_factor*ds_x*lattice_x));
                 }
				 else {}
                 }
  }               
}
else
{if(strcmp(component,"Ex")==0)
		for(k=k_range;k>=1;k--)
		{
				for(i=1;i<=i_range;i++)	fprintf(file,"%g ",sqrt(Ex_yplane_measure_cos[i][k][1]*Ex_yplane_measure_cos[i][k][1]+Ex_yplane_measure_sin[i][k][1]*Ex_yplane_measure_sin[i][k][1]));
				fprintf(file,"\n");
		}
else if (strcmp(component,"Ez")==0)
		for(k=k_range;k>=1;k--)
		{
				for(i=1;i<=i_range;i++)	fprintf(file,"%g ",sqrt(Ez_yplane_measure_cos[i][k][1]*Ez_yplane_measure_cos[i][k][1]+Ez_yplane_measure_sin[i][k][1]*Ez_yplane_measure_sin[i][k][1]));
				fprintf(file,"\n");
		}
else if (strcmp(component,"Hy")==0)
		for(k=k_range;k>=1;k--)
		{
				for(i=1;i<=i_range;i++)	fprintf(file,"%g ",sqrt(Hy_yplane_measure_cos[i][k][1]*Hy_yplane_measure_cos[i][k][1]+Hy_yplane_measure_sin[i][k][1]*Hy_yplane_measure_sin[i][k][1]));
				fprintf(file,"\n");
		}
else {}		
	}
}
	if(strcmp("z",plane)==0)
	{
		k=floor(0.5+((value+zcenter)*lattice_z));
		for(j=j_range;j>=1;j--)
		{
			for(i=1;i<=i_range;i++) fprintf(file,"%g ",grid_value(component,i,j,k));	
			fprintf(file,"\n");
		}
	}
	fclose(file);
	printf("out %s...ok\n",component);
}

float grid_value(char *component,int i,int j,int k)
{
	if(strcmp(component,"Ex")==0) return efieldx(i,j,k);
	if(strcmp(component,"Ey")==0) return efieldy(i,j,k);
	if(strcmp(component,"Ez")==0) return efieldz(i,j,k);
	if(strcmp(component,"Hx")==0) return hfieldx(i,j,k);
	if(strcmp(component,"Hy")==0) return hfieldy(i,j,k);
	if(strcmp(component,"Hz")==0) return hfieldz(i,j,k);
}

float ***float_3d_memory(int imax,int jmax,int kmax)
{
	int i,j,k;
	float ***memory,*cmemory;

	cmemory=(float *)malloc(sizeof(float)*imax*jmax*kmax);
	memory=(float ***)malloc(sizeof(float **)*imax);
	for(i=0;i<imax;i++)
	{
		memory[i]=(float **)malloc(sizeof(float *)*jmax);
		for(j=0;j<jmax;j++)
		{
			memory[i][j]=cmemory+i*jmax*kmax+j*kmax;
			for(k=0;k<kmax;k++)	memory[i][j][k]=0.0;
		}
	}

	return memory;
}

float hfieldx(int i,int j,int k)
{return (Hx[i][j][k]+Hx[i][j-1][k]+Hx[i][j][k-1]+Hx[i][j-1][k-1])/4;
}

float hfieldy(int i,int j,int k)
{return (Hy[i][j][k]+Hy[i][j][k-1]+Hy[i-1][j][k]+Hy[i-1][j][k-1])/4;
}

float hfieldz(int i,int j,int k)
{return (Hz[i][j][k]+Hz[i-1][j][k]+Hz[i][j-1][k]+Hz[i-1][j-1][k])/4;
}

float efieldx(int i,int j,int k)
{return (Ex[i][j][k]+Ex[i-1][j][k])/2;
}

float efieldy(int i,int j,int k)
{return (Ey[i][j][k]+Ey[i][j-1][k])/2;
}

float efieldz(int i,int j,int k)
{return (Ez[i][j][k]+Ez[i][j][k-1])/2;
}
Member Avatar
Ancient Dragon
Achieved Level 70
27,572 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

One of the problems is that line 55-66 is calling that function with too few arguments. float_3d_memory() takes three arguments, not two.

Pay close attention to the errors and warnings that your compiler gives you. If it doesn't give you any then you need to increase the warning level. How to do that depends on the compiler you are using. I used vc++ 2010 express on Windows 7 and got 26 errors like the one I mentioned above.

Member Avatar
johnnydarten
Newbie Poster
21 posts since May 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

You are right. I added 1 in the 3rd field to be(line 55):
Ex_yplane_measure_cos=float_3d_memory(isize,ksize,1);
as well as the next 5 lines. However still it crashes...This time Dev C++ 's C compiler didn't give me any warning...

Member Avatar
Ancient Dragon
Achieved Level 70
27,572 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

The problem is the loops in out_plane(), loop counters exceeding the bounds of those arrays.

void out_plane(char *component,char *plane,float value,char *lastname,long tt)
{
	FILE *file;
	char name[20];
	int i,j,k;
	int i_range, j_range, k_range; 
	// for Hz_parity, normal cases
	i_range = isize-1;
	j_range = jsize-1;
	k_range = ksize-1; 
	
	if(use_periodic_x == 1)
		i_range = isize;
	if(use_periodic_y == 1)
		j_range = jsize; 
	
	sprintf(name,"%07d",tt);
	strcat(name,lastname);
	file=fopen(name,"w");

	if(strcmp("x",plane)==0)
	{
		i=floor(0.5+((value+xcenter)*lattice_x));
		for(k=k_range-1;k>=1;k--)
		{
				for(j=1;j<j_range;j++)	fprintf(file,"%g ",grid_value(component,i,j,k));
				fprintf(file,"\n");
		}
	}

	if(strcmp("y",plane)==0)
	{
		j=floor(0.5+((value+ycenter)*lattice_y));
if(tt<tendrecord)
{
 for (k=k_range-1;k>=0;k--)
 {
				for(i=1;i<i_range;i++)	
				{if(strcmp(component,"Ex")==0)
                 {Ex_yplane_measure_cos[i][k][1]+=grid_value(component,i,j,k)*cos(2*pi*freqmeasure*tt*(a_n*1E-9)/(light_speed*S_factor*ds_x*lattice_x));
                 Ex_yplane_measure_sin[i][k][1]+=grid_value(component,i,j,k)*sin(2*pi*freqmeasure*tt*(a_n*1E-9)/(light_speed*S_factor*ds_x*lattice_x));
                 }
                 else if(strcmp(component,"Ez")==0)
				 {Ez_yplane_measure_cos[i][k][1]+=grid_value(component,i,j,k)*cos(2*pi*freqmeasure*tt*(a_n*1E-9)/(light_speed*S_factor*ds_x*lattice_x));
				 Ez_yplane_measure_sin[i][k][1]+=grid_value(component,i,j,k)*sin(2*pi*freqmeasure*tt*(a_n*1E-9)/(light_speed*S_factor*ds_x*lattice_x));
                 }
				 else if(strcmp(component,"Hy")==0)
				 {//////IF ERROR IN RESULT LOOK AT FFT OF FARFIELD.C
                 Hy_yplane_measure_cos[i][k][1]+=grid_value(component,i,j,k)*cos(2*pi*freqmeasure*tt*(a_n*1E-9)/(light_speed*S_factor*ds_x*lattice_x));
                 Hy_yplane_measure_sin[i][k][1]+=grid_value(component,i,j,k)*sin(2*pi*freqmeasure*tt*(a_n*1E-9)/(light_speed*S_factor*ds_x*lattice_x));
                 }
				 else {}
                 }
  }               
}
else
{if(strcmp(component,"Ex")==0)
		for(k=k_range-1;k>=1;k--)
		{
				for(i=1;i<i_range;i++)	fprintf(file,"%g ",sqrt(Ex_yplane_measure_cos[i][k][1]*Ex_yplane_measure_cos[i][k][1]+Ex_yplane_measure_sin[i][k][1]*Ex_yplane_measure_sin[i][k][1]));
				fprintf(file,"\n");
		}
else if (strcmp(component,"Ez")==0)
		for(k=k_range-1;k>=1;k--)
		{
				for(i=1;i<i_range;i++)	fprintf(file,"%g ",sqrt(Ez_yplane_measure_cos[i][k][1]*Ez_yplane_measure_cos[i][k][1]+Ez_yplane_measure_sin[i][k][1]*Ez_yplane_measure_sin[i][k][1]));
				fprintf(file,"\n");
		}
else if (strcmp(component,"Hy")==0)
		for(k=k_range-1;k>=1;k--)
		{
				for(i=1;i<i_range;i++)	fprintf(file,"%g ",sqrt(Hy_yplane_measure_cos[i][k][1]*Hy_yplane_measure_cos[i][k][1]+Hy_yplane_measure_sin[i][k][1]*Hy_yplane_measure_sin[i][k][1]));
				fprintf(file,"\n");
		}
else {}		
	}
}
	if(strcmp("z",plane)==0)
	{
		k=floor(0.5+((value+zcenter)*lattice_z));
		for(j=j_range-1;j>=1;j--)
		{
			for(i=1;i<i_range;i++) fprintf(file,"%g ",grid_value(component,i,j,k));	
			fprintf(file,"\n");
		}
	}
	fclose(file);
	printf("out %s...ok\n",component);
}
Member Avatar
johnnydarten
Newbie Poster
21 posts since May 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you so much.

Johnnydarten

Question Answered as of 2 Years Ago by Ancient Dragon
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: