发生“分段错误”的错误 [英] errors with "segmentation fault occurred "

查看:120
本文介绍了发生“分段错误”的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些写在文件4中的值,我需要它们再次调用新的计算,但是在读线read(4,*)NNrow(I),Niz(I),NNbin(I) ,当我想运行代码时,我收到错误分段错误发生,我的Nfi(I),NfiStdDev(I),NfiAvr(I),NMagbin(I),Nzup可以再次使用这个文件吗?

  do j = 1,nmax 
if(zb(iz,im,j) .ne.0),然后
调用Romberg(dix,dDistCa,zb(iz,im,j),zup(iz))!COMOVING DISTANCE
Vmax = dix * S!COMOVINF VOLUME
fi = fi + 1 / Vmax!亮度函数
写(2,'(i5,2x,f9.4,2x,f8.5,2x,3f14.10)')j,magbin,zbin,S, Vmax,dix
endif

enddo

if(Nbin.ge.n_thresh)then
Nrow = Nrow + 1
write(4 ,'(3i7,2x,f25.8,2x,2f20.8,2x,f9.4,2x中,f 8.5)')Nrow,iz,Nbin,fi,fiStdDev,fiAvr,magbin,zup(iz)
endif
enddo loopmag

倒带(4)
close (4)


写(*,*)Nrow
open(4,file ='luminosity_func_I.asc')

分配(fiStdDev2( ),
分配(fi_expected(Nrow),stat = ok)
分配(DFI(Nrow),stat = ok)
分配(CHISQ(Nrow),stat =好的)
! Ln10 = 2.3025
A = 0.4 * 2.3025
do I = 1,Nrow! NDATA = NMAX
write(*,*)I
read(4,*)NNrow(I),Niz(I),NNbin(I),Nfi(I),NfiStdDev(I),NfiAvr (I),NMagbin(I),Nzup(I)

fiStdDev2(I)= 1 / NfiStdDev(I)* NfiStdDev(I)
write(*,*)fiStdDev2 )(*)* *(*)* *(* * * * * * * * * * * * * * *我))))
DFI(I)= fi_expected(I)-NFI(I)
CHISQ(I)= DFI(I)* DFI(I)* fiStdDev2(I)
END DO


解决方案

据我所见,在读取语句中出现两种情况:


  1. 您是试图存储数组之外的信息,即 i>大小(<一个数组>)。你可以用 -fbounds-check 来检查 gfortran - 检查界限 code>为 ifort


    • 从文件中读取内容时出现错误:

      单位很低,你可以访问一个保留单位 - 尝试类似于 1234 。另请参阅此帖:针对ansys的linux分段错误

    • 您在文件末尾阅读

    • 没有足够的列可以从文件中读取

  2. >

您可以将 iostat = ierror 放入您的 code>语句来检查在读入时是否发生了错误。 ierror <0 意味着您正在尝试读取超出文件末尾的内容,而 ierror> 0 对应于读取文件时的错误。

I have some values written in file 4 and I need them to call again for new calculations but I have some problem in read line " read (4,*) NNrow(I),Niz(I),NNbin(I),Nfi(I),NfiStdDev(I),NfiAvr(I),NMagbin(I),Nzup(I) " when I want to run the code I'm receiving the error "segmentation fault occurred" how I can use this file again?

                                 do j=1,nmax
                                    if (zb(iz,im,j).ne.0) then
              call Romberg (dix,dDistCa,zb(iz,im,j),zup(iz))                    !COMOVING DISTANCE
              Vmax=dix*S                                    !COMOVINF VOLUME
              fi=fi+1/Vmax                                    !LUMINOSITY FUNCTION
 write(2,'(i5,2x,f9.4,2x,f8.5,2x,3f14.10)')j,magbin,zbin,S,Vmax,dix
            endif

        enddo

            if (Nbin.ge.n_thresh) then
    Nrow=Nrow+1
write(4,'(3i7,2x,f25.8,2x,2f20.8,2x,f9.4,2x,f8.5)')Nrow,iz,Nbin,fi,fiStdDev,fiAvr,magbin,zup(iz)
            endif
              enddo loopmag

rewind(4)
close(4)


write(*,*)Nrow 
 open(4,file='luminosity_func_I.asc')

 allocate (fiStdDev2(Nrow),stat=ok)
 allocate (fi_expected(Nrow),stat=ok)
 allocate (DFI(Nrow),stat=ok)
 allocate (CHISQ(Nrow),stat=ok)
 ! Ln10=2.3025
 A=0.4*2.3025
            do I=1,Nrow        ! NDATA=NMAX
            write(*,*)I
                read (4,*) NNrow(I),Niz(I),NNbin(I),Nfi(I),NfiStdDev(I),NfiAvr(I),NMagbin(I),Nzup(I)

                  fiStdDev2(I)=1/NfiStdDev(I)*NfiStdDev(I)
            write(*,*)fiStdDev2(I)
                  fi_expected(I)=A*fi_star*10**(0.4*(alpha+1)*(M_star-NMagbin(I)))*exp(-10**(0.4*(M_star-NMagbin(I))))
                  DFI(I)=fi_expected(I)-NFI(I)
                  CHISQ(I)=DFI(I)*DFI(I)*fiStdDev2(I)
            END DO

解决方案

As far as I can see, there could be two things going wrong in that read statement:

  1. You are trying to store the information outside the arrays, i.e. i > size(<one of the arrays>). You can check for this with -fbounds-check for gfortran and -check bounds for ifort.

  2. There's something wrong while reading in from the file:

    • The unit is pretty low, you could access a reserved unit - try something like 1234. See also this post: segmentation error in linux for ansys
    • You read beyond the end of the file
    • There are not enough columns to read from the file

You could put iostat=ierror into your read statement to check whether an error occured while reading in. ierror<0 would mean that you are trying to read beyond the end of the file, while ierror>0 corresponds to an error while reading the file.

这篇关于发生“分段错误”的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆