【发布时间】:2015-07-20 22:14:03
【问题描述】:
我无法忍受编程任务。我为正常工作的单色波(用于一个输入频率)编写了一个简单的散射代码,涉及将数据保存在文件中。为此,我使用 6 个动态分配的数组。如果我想扩展脉冲,我必须计算不同频率的单色波并将它们相加。所以在这一步中,我创建了另外 6 个数组作为临时数组,并将它们添加到“最终”数组中。如果我只是打印这些最终的数组就可以了,但是如果我调用一个 fopen 函数,那么它就会损坏。使用 Valgrind 并没有太大帮助,因为它只是说:
--6199-- VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) - exiting
--6199-- si_code=1; Faulting address: 0x79B1F4E8; sp: 0x68152e40
valgrind: the 'impossible' happened:
Killed by fatal signal
host stacktrace:
==6199== at 0x38066BAA: ??? (in /usr/lib/valgrind/memcheck-x86-linux)
sched status:
running_tid=1
Thread 1: status = VgTs_Runnable
为简单起见,我只给你我的代码的重要部分。 bhmie.c:
int main(void)
{
int fiang, thang,rstep;
fiang=3; //Resolution of phi coordinate
thang=3; //Resolution of Theta coordinate
rstep=3; //Resolution of Rho coordinate
int felosz=rstep*thang*fiang;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Initialization of arrays
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
double complex *Er,*Ertemp;
double complex *Eth,*Ethtemp;
double complex *Efi,*Efitemp;
double complex *Hr,*Hrtemp;
double complex *Hth,*Hthtemp;
double complex *Hfi,*Hfitemp;
double *th;
double *fi;
double *r;
Er = (double complex*) malloc(sizeof(double complex)*felosz);
Eth = (double complex*) malloc(sizeof(double complex)*felosz);
Efi = (double complex*) malloc(sizeof(double complex)*felosz);
Hr = (double complex*) malloc(sizeof(double complex)*felosz);
Hth = (double complex*) malloc(sizeof(double complex)*felosz);
Hfi = (double complex*) malloc(sizeof(double complex)*felosz);
Ertemp = (double complex*) malloc(sizeof(double complex)*felosz);
Ethtemp = (double complex*) malloc(sizeof(double complex)*felosz);
Efitemp = (double complex*) malloc(sizeof(double complex)*felosz);
Hrtemp = (double complex*) malloc(sizeof(double complex)*felosz);
Hthtemp = (double complex*) malloc(sizeof(double complex)*felosz);
Hfitemp = (double complex*) malloc(sizeof(double complex)*felosz);
r =(double*) malloc(rstep * sizeof(double));
th=(double*) malloc(thang * sizeof(double));
fi=(double*) malloc(fiang * sizeof(double));
zerolist(Er,felosz); //set all value for 0.0 + I* 0.0
zerolist(Eth,felosz);
zerolist(Efi,felosz);
zerolist(Hr,felosz);
zerolist(Hfi,felosz);
zerolist(Hth,felosz);
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Extension for pulse
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int levag = 5; //truncate of pulse
int FN =pow(2,4); //frequency resolution
fftw_complex *impx, *impy;
impx =(fftw_complex*) fftw_malloc ( sizeof ( fftw_complex ) * FN );
impy =(fftw_complex*) fftw_malloc ( sizeof ( fftw_complex ) * FN );
fftimpulse(ex,freq,fwhm,levag,FN,impx);
fftimpulse(ey,freq,fwhm,levag,FN,impy);
double *vecfreq; //the array which contains the frequencies
vecfreq=(double*) malloc(thang * sizeof(double));
freqscale(FN,fwhm,levag,freq,vecfreq);
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//calculation
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int kl,xy;
for(kl=0; kl<FN; kl++)
{
nearfield(vecfreq[kl],impx[kl],impy[kl],rstep, thang, fiang,r,th,fi,Ertemp,Ethtemp,Efitemp,Hrtemp,Hthtemp,Hfitemp);
for(xy=0 ; xy<felosz; xy++)
{
*(Er+xy) += *(Ertemp+xy);
*(Eth+xy) += *(Ethtemp+xy);
*(Efi+xy) += *(Efitemp+xy);
*(Hr+xy) += *(Hrtemp+xy);
*(Hth+xy) += *(Hthtemp+xy);
*(Hfi+xy) += *(Hfitemp+xy);
}
}
Ertemp=NULL;
Efitemp=NULL;
Ethtemp=NULL;
Hrtemp=NULL;
Hfitemp=NULL;
Hthtemp=NULL;
free(Ertemp);
free(Ethtemp);
free(Efitemp);
free(Hrtemp);
free(Hthtemp);
free(Hfitemp);
savingvfield(rstep,thang,fiang,r,th,fi,Er,Eth,Efi,Hr,Hth,Hfi);
printf("Everything OK \n");
return 0;
}
进一步评论:如果我不 NULL 临时数组,我不能使用 free() 函数。我不知道为什么...
问题出在 savevfield 函数上:
void savingvfield(int rstep,int thang, int fiang,double *r,double *th,double *fi,double complex *Er,double complex *Eth,double complex *Efi,double complex *Hr,double complex *Hth,double complex *Hfi)
{
FILE *nearscatt;
static char filename2[100];
sprintf(filename2, "nearscatt-ord_%i_x_%i.dat",rstep,than);
nearscatt = fopen("nearscatt-ord.dat","wb");
int i,j,k;
for (i=0; i<rstep; i++)
{
for (j=0; j<thang; j++)
{
for (k=0; k<fiang; k++)
{
fprintf(nearscatt," %g \t %g \t %g \t %g \t %g \t %g \t %g \t %g \t %g \t %g \t %g \t %g \t %g \t %g \t %g \n ",
r[i]*sin(th[j])*cos(fi[k])*1.0e+9,
r[i]*sin(th[j])*sin(fi[k])*1.0e+9,
r[i]*cos(th[j])*1.0e+9,
creal(Er[k + fiang * (j + thang * i)]*sin(th[j])*cos(fi[k])+Eth[k + fiang * (j + thang * i)]*cos(th[j])*cos(fi[k])-Efi[k + fiang * (j + thang * i)]*sin(fi[k])),
cimag(Er[k + fiang * (j + thang * i)]*sin(th[j])*cos(fi[k])+Eth[k + fiang * (j + thang * i)]*cos(th[j])*cos(fi[k])-Efi[k + fiang * (j + thang * i)]*sin(fi[k])),
creal(Er[k + fiang * (j + thang * i)]*sin(th[j])*sin(fi[k])+Eth[k + fiang * (j + thang * i)]*cos(th[j])*sin(fi[k])+Efi[k + fiang * (j + thang * i)]*cos(fi[k])),
cimag(Er[k + fiang * (j + thang * i)]*sin(th[j])*sin(fi[k])+Eth[k + fiang * (j + thang * i)]*cos(th[j])*sin(fi[k])+Efi[k + fiang * (j + thang * i)]*cos(fi[k])),
creal(Er[k + fiang * (j + thang * i)]*cos(th[j])-Eth[k + fiang * (j + thang * i)]*sin(th[j])),
cimag(Er[k + fiang * (j + thang * i)]*cos(th[j])-Eth[k + fiang * (j + thang * i)]*sin(th[j])),
creal(Hr[k + fiang * (j + thang * i)]*sin(th[j])*cos(fi[k])+Hth[k + fiang * (j + thang * i)]*cos(th[j])*cos(fi[k])-Hfi[k + fiang * (j + thang * i)]*sin(fi[k])),
cimag(Hr[k + fiang * (j + thang * i)]*sin(th[j])*cos(fi[k])+Hth[k + fiang * (j + thang * i)]*cos(th[j])*cos(fi[k])-Hfi[k + fiang * (j + thang * i)]*sin(fi[k])),
creal(Hr[k + fiang * (j + thang * i)]*sin(th[j])*sin(fi[k])+Hth[k + fiang * (j + thang * i)]*cos(th[j])*sin(fi[k])+Hfi[k + fiang * (j + thang * i)]*cos(fi[k])),
cimag(Hr[k + fiang * (j + thang * i)]*sin(th[j])*sin(fi[k])+Hth[k + fiang * (j + thang * i)]*cos(th[j])*sin(fi[k])+Hfi[k + fiang * (j + thang * i)]*cos(fi[k])),
creal(Hr[k + fiang * (j + thang * i)]*cos(th[j])-Hth[k + fiang * (j + thang * i)]*sin(th[j])),
cimag(Hr[k + fiang * (j + thang * i)]*cos(th[j])-Hth[k + fiang * (j + thang * i)]*sin(th[j]))
);
}
}
}
fclose(nearscatt);
}
当我进行调试时,当到达 nearscatt = fopen("nearscatt-ord.dat","wb"); 行时出现错误,但如果我删除此文件内保存行,并且将 fprintf 替换为 printf(更改适当形式的参数)它正常运行并给出合理的输出。
代码nearfield.c :
void nearfield(double freq, double impx, double impy, int rstep,int thang,int fiang,double *r,double *th,double *fi,double complex *Er,double complex *Eth,double complex *Efi,double complex *Hr,double complex *Hth,double complex *Hfi)
{
double complex Esumr,Esumth,Esumfi;
double complex Hsumr,Hsumth,Hsumfi;
double complex M1R,M1Theta,M1Phi;
double complex HM1R,HM1Theta,HM1Phi;
int i,j,k;
for (i=0; i<rstep; i++)
{
r[i]=rmin+i*rinc;
for (j=0; j<thang; j++)
{
th[j]=j*tinc + 0.001;
for (k=0; k<fiang; k++)
{
Esumr = 0.,Esumth= 0.,Esumfi = 0.;
Hsumr = 0.,Hsumth= 0.,Hsumfi = 0.;
qback = 0.,qsca=0.,qext=0.;
fi[k]=k*finc;
Er[k+fiang*(j+thang*i)]=0.+I*0.;
Eth[k+fiang*(j+thang*i)]=0.+I*0.;
Efi[k+fiang*(j+thang*i)]=0.+I*0.;
Hr[k+fiang*(j+thang*i)]=0.+I*0.;
Hth[k+fiang*(j+thang*i)]=0.+I*0.;
Hfi[k+fiang*(j+thang*i)]=0.+I*0.;
for (n=1; n<=N; n++)
{
M1R = ...somefunction...
M1Theta = ...somefunction...
M1Phi = ...somefunction...
HM1R = ...somefunction...
HM1Theta = ...somefunction...
HM1Phi = ...somefunction...
Esumr += cpow(1*I,n+1)*(2*n+1)*M1R;
Esumth += cpow(1*I,n)*(2*n+1)/(n*(n+1))*M1Theta;
Esumfi += cpow(1*I,n)*(2*n+1)/(n*(n+1))*M1Phi;
Hsumr += kk/(mu0*2*M_PI*freq)* cpow(1*I,n+1)*(2*n+1)*HM1R;
Hsumth += kk/(mu0*2*M_PI*freq)*cpow(1*I,n)*(2*n+1)/(n*(n+1))*HM1Theta;
Hsumfi += kk/(mu0*2*M_PI*freq)*cpow(1*I,n)*(2*n+1)/(n*(n+1))*HM1Phi;
Er[k+fiang*(j+thang*i)]=Esumr;
Eth[k+fiang*(j+thang*i)]=Esumth;
Efi[k+fiang*(j+thang*i)]=Esumfi;
Hr[k+fiang*(j+thang*i)]=Hsumr;
Hth[k+fiang*(j+thang*i)]=Hsumth;
Hfi[k+fiang*(j+thang*i)]=Hsumfi;
}
}
}
}
}
运行良好,没有任何困难。 我想强调的是,它适用于简单的频率(保存)和脉冲(仅打印)。所以唯一的问题是:我怎样才能将我的 Er、Eth、Efi、Hr、Hth、Hfi 保存到文件中而不会损坏内存? 作为第一个猜测,我认为它是 double complex 类型的东西。 如果您需要任何进一步的信息,请询问! 感谢您的帮助!
【问题讨论】:
-
"如果我不将临时数组设为 NULL,我将无法使用 free() 函数" -- 允许使用
NULL值调用free,在这种情况下它什么也不做。这意味着您使用这些完全相同的临时数组是一个问题。 -
所以你建议我必须忽略NULL和free()?但在那部分之后我不想使用它们。我试过没有它们,但我遇到了同样的问题。
-
如果 any 之一
free失败,则写入这些数组的代码有问题。free本身应该可以工作。 -
我发现了问题。我在 vecfreq 中分配了错误的大小。而不是
FN,我使用thang。谢谢你的好意
标签: c arrays memory malloc fopen