errors with “segmentation fault occurred ”

不羁的心 提交于 2019-12-02 22:33:13

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/19400039/errors-with-segmentation-fault-occurred

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!