c语言 - 内存损坏,当我想在文件中写入数组时



我无法承担编程任务。我为单色波(对于一个输入频率)编写了一个简单的散射代码,该代码正常工作,涉及将数据保存在文件中。为此,我使用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;
}

进一步的评论:如果我不将温度阵列删除,我将无法使用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);

}

当我进行调试时,我会在访问line近距离时会出错将FPRINTF替换为printf(更改适当表格的参数),它正常运行并提供明智的输出。

代码近菲尔德.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,HTH,HFI保存到没有内存损坏的文件中?第一个猜测,我认为这是类型双重复杂。如果您需要进一步的信息,请询问!谢谢您的帮助!

为VecfReq提供了错误的大小。我使用thang

而不是fn

相关内容

  • 没有找到相关文章

最新更新