当我想在文件

时间:2015-05-10 10:17:44

标签: c arrays memory malloc fopen

我无法忍受编程任务。我为单色波(一个输入频率)写了一个简单的散射码,它正常工作,包括将数据保存在文件中。为此,我使用了6个动态分配的数组。如果我想延长脉冲,我必须计算不同频率的单色波并对它们求和。因此,在这一步中,我创建了另外6个数组作为临时数组,并将它们添加到" final"阵列。如果我只是打印这些最终数组就可以,但是如果我调用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;
}

进一步评论:如果我没有使临时数组为空,我就不能使用free()函数。我不知道为什么......

问题在于 savingvfield

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(&#34; nearscatt-ord.dat&#34;,&#34; wb&#34;); 时会出现错误,但是如果我删除了这个文件内保存行,并将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保存到没有内存损坏的文件中? 作为第一个猜测,我认为它是类型双复合的东西.. 如果您需要任何进一步的信息,请询问! 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

vecfreq的尺寸错误。 而不是FN我使用了thang