简体   繁体   English

Fortran gfortran linux中的“Segmentation Fault(core dumped)”错误

[英]“Segmentation Fault (core dumped)” error in Fortran gfortran linux

I am creating a program which will analyse a file .fits in a directory and then it will create another file .txt in another directory. 我正在创建一个程序,它将分析目录中的文件.fits然后它将在另一个目录中创建另一个文件.txt。 It is just a converter. 它只是一个转换器。

When I tried to execute the program (compiling is OK) it gives me an error message: 当我尝试执行程序(编译正常)时,它给出了一条错误消息:

Program received signal SIGSEGV: Segmentation fault - invalid memory reference. 程序接收信号SIGSEGV:分段故障 - 无效的存储器参考。

Backtrace for this error: 0 0x7FC5ADB1C117 1 0x7FC5ADB1C6F4 2 0x7FC5AD46C0AF 3 0x44E704 in ffthdu at fitscore.c:6893 4 0x405101 in MAIN__ at Codigo.f90:? 回溯此错误:0 0x7FC5ADB1C117 1 0x7FC5ADB1C6F4 2 0x7FC5AD46C0AF 3 0x44E704在ffthdu at fitscore.c:6893 4 0x405101在MAIN__中的Codigo.f90:? Segmentation fault (core dumped) 分段故障(核心转储)

And there is another information: when I delete a stretch of the program which does the "write to the new .txt converter file" part, the program goes OK and it reads the entire .fits file! 还有另一个信息:当我删除一段程序“写入新的.txt转换文件”部分时,程序运行正常,它会读取整个.fits文件! Here is this stretch: 这是这段时间:

arq='spec-1.txt'
open (29,file=arq,status='unknown')
write(29,*) '  l(n)                      fa(n)'
do n=1,naxes
   write(29,*) l(n), fa(n)
end do

I have declared the variable "arq" as a character and everything is OK. 我已将变量“arq”声明为一个字符,一切正常。 When I delete this stretch the segmentation fault does not appear any more, but I do not know what to do now because I need to pass the information to this .txt! 当我删除此拉伸时,分段错误不再出现,但我现在不知道该怎么做因为我需要将信息传递给此.txt! And the "arq" string is not complete because I am just testing now, later I will add the entire directory. 并且“arq”字符串不完整,因为我现在只是测试,稍后我将添加整个目录。

And here is the full program, I am using "gfortran Codigo.f90 -o TESTE -Lcfitsio -lcfitsio" to compile it: 这是完整的程序,我使用“gfortran Codigo.f90 -o TESTE -Lcfitsio -lcfitsio”来编译它:

implicit none

integer largo,status,hdnum,n,keysexist,keysadd,bitpix,naxis
integer naxes,readwrite,blocksize
integer i,j,k
integer iF(3),iT(3),iw(3),iwe,il,ib,jb,iFold,iFoldmin(3)
integer iFoldmax(3),iFoldopt(3)
integer iMiMy,iMo,Tymin,Tymax,Timin,Timax,Tyoung(3),Tint(3),Told
integer i912,i45000,Tyin,Tiin,nrows,ncols,datacode,repeat,width
integer nlam_ext
integer fKD,fKK,fKKD
parameter (nlam_ext = 1145)
CHARACTER NAMECH*256,name2*256,alibi*1000,card*80,code*14
CHARACTER arq*1000
CHARACTER name(7)*256,namein*256,nebname*256,nameout*256
CHARACTER nameindva*256,nameoutdva*256,mid(3)*4,line*1000
real fa(10000),fcs(10000),noiz(10000),mask(10000)
real*8 l(10000)
real*8 w_ext(nlam_ext),alam_ext(nlam_ext)
REAL*8 F(3,7),T(3,35),Fl(3,7,36,6900),w(3,6900),y(6900),ye(6900)
REAL*8 SN(6900),xe(6900)
real*8 CHISQU,CHISQUmin(3),CHISQUup,CHISQUdown,CHISQUmid,nf,nfup
real*8 nfdown,nfmid,nfopt(3)
real*8 t4020,y4020,i4020,o4020,t4860,y4860,i4860,o4860
real*8 xd(10000),yd(10000),absorb,ebminv,ke,redshift
real*8 xf(10000),yf(10000),yp(10000)
real*8 x(6900)
real*8 flueks(10000),extcurve(10000)
real*8 xg,yg(10000),x2,y2(10000)
REAL*8 sng,ewa,ewb,ewg,hha,hhb,oldage(3)
REAL*8 Mo(9),MiMy(9),Myoung,Mint,Mold,Myopt(3),Miopt(3),Moopt(3)
REAL*8 Fyoung,Fint,Fold(7),Noise
real*8 a,b,c, plyus,minus,xx,nebemm(6900),yy(6900)
real*8 Flyoung(6900),Flint(6900),Flold(6900)
real*8 ha,Oiii4959,Oiii5007,Nii,Nii2,Sii6717,Sii6731,Oii3727,Hei
real*8 hhg,hg,Oiii4363,Oii7319,Oii7330,Nii6548,Nii6584
real*8 R23,R3,P,XNII,xis,yps,O3N2,R,Z,k0,k1,k2,q,logq
real*8 OHNII,OHZ,OHMcG,OHP,OHKD,I2lOH,OHKK
real*8 OHKKD,OHKKbe,OHPPN2,OHPPO3N2,OHP05,I2lOHbef
logical anynull
  ebminv = 0.
namech='/home/matheus/Desktop/IC/Spectra/Elliptical/spec-0266-51602-0467.fits'
write(6,*)namech
readwrite=0
status=0
call ftopen(17,namech,readwrite,blocksize,status)
call ftghsp(17,keysexist,keysadd,status)
do n=1,keysexist
  call ftgrec(17,n,card,status)
  if (card(1:9)=='CRVAL1  =') then
    read(card,'(a9,e21.14)')alibi,a
  endif
  if (card(1:6)=='NAXIS1') then
    read(card,'(a9,i21)')alibi,
   endif
  if (card(1:6)=='Z     ') then
    read(card,'(a9,e21.14)')alibi,redshift
  endif
  if (card(1:6)=='SN_G  ') then
    read(card,'(a9,e21.14)')alibi,sng 
  endif
enddo
l(1)=10.**a/(1.+redshift)
l(naxes)=10.**(a+0.0001*float(naxes-1))/(1.+redshift)
  do n=1,naxes
  l(n)=10.**(a+0.0001*float(n-1))
enddo
 call ftthdu(17,hdnum,status)
call ftgidt(17,bitpix,status)
call ftgtcl(17,1,datacode,repeat,width,status)
call ftgpve(17,1,1,naxes,0.,fa,anynull,status)
call ftgpve(17,1,naxes+1,naxes,0.,fcs,anynull,status)
call ftgpve(17,1,2*naxes+1,naxes,0.,noiz,anynull,status)
call ftgpve(17,1,3*naxes+1,naxes,0.,mask,anynull,status)
call ftclos(17,status) 
write(6,*)'spectra read in',l(1),l(naxes)
arq='spec-1.txt'
open (29,file=arq,status='unknown')
write(29,*) '  l(n)','                      fa(n)'
do n=1,naxes
   write(29,*) l(n), fa(n)
end do


 close(25)
write(6,*)
write(6,'(a)')'Justice is done'
 end

Actually the error happens because it gives to naxes always a zero value! 实际上错误发生是因为它给naxes总是零值!

The most frequent causes of fatal memory errors in Fortran are illegal array subscripts and inconsistency between the arguments of the call to a procedure (subroutine or function) and what the declared arguments of the procedure. Fortran中致命内存错误的最常见原因是非法数组下标以及对过程(子例程或函数)的调用参数与过程的声明参数之间的不一致。 For the first, turn on run-time subscript checking. 首先,打开运行时下标检查。 With gfortran, -fcheck=bounds , or better, turn on additional run-time checks with -fcheck=all . 使用gfortran, -fcheck=bounds或更好,使用-fcheck=all打开其他运行时检查。 For the procedure argument issue, place all of your procedures into module(s) and use those module(s) from any routine that calls any of the procedures. 对于过程参数问题,将所有过程放入模块中,并use任何调用任何过程的例程中的模块。 This will enable the compiler to check argument consistency at compile time. 这将使编译器能够在编译时检查参数一致性。

Thank you all! 谢谢你们! I add -fcheck=all to see what is happening wrong when it tries to execute the program and I got that: 我添加-fcheck = all以查看在尝试执行程序时发生的错误,我得到了:

At line 65 of file Codigo.f90 Fortran runtime error: Index '0' of dimension 1 of array 'l' below lower bound of 1 在文件Codigo.f90的第65行Fortran运行时错误:数组'l'的维度1的索引'0'低于1的下限

Actually I think that the problem is with the "naxes"! 实际上我认为问题在于“纳克斯”! Because when I compile it substituting "naxes" to "2", for example, it does not give me the results (of course) but the error goes away! 因为当我编译它时将“naxes”替换为“2”,例如,它不会给我结果(当然)但是错误消失了!

Does someone can maybe tell me why is this error with "naxes" happening?? 有人可以告诉我为什么这个错误与“纳克斯”发生? Thank you all again!!! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM