Fortran gfortran linux 中的“分段错误(核心转储)”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20453315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:34:50  来源:igfitidea点击:

"Segmentation Fault (core dumped)" error in Fortran gfortran linux

linuxfortran

提问by Matheus Danella

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. It is just a converter.

我正在创建一个程序,它将分析一个目录中的文件 .fits,然后它将在另一个目录中创建另一个文件 .txt。它只是一个转换器。

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:? Segmentation fault (core dumped)

此错误的回溯:0 0x7FC5ADB1C117 1 0x7FC5ADB1C6F4 2 0x7FC5AD46C0AF 3 0x44E704 in ffthdu at fitscore.c:6893 4 0x405101 in MAIN__ at Codigo: 分段错误(核心转储)

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! Here is this stretch:

还有另一个信息:当我删除一段执行“写入新的 .txt 转换器文件”部分的程序时,程序运行正常并读取整个 .fits 文件!这是这个延伸:

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. 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! And the "arq" string is not complete because I am just testing now, later I will add the entire directory.

我已将变量“arq”声明为一个字符,一切正常。当我删除此拉伸时,分段错误不再出现,但我现在不知道该怎么做,因为我需要将信息传递给这个 .txt!并且“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 总是一个零值!

采纳答案by M. S. B.

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. For the first, turn on run-time subscript checking. With gfortran, -fcheck=bounds, or better, turn on additional run-time checks with -fcheck=all. For the procedure argument issue, place all of your procedures into module(s) and usethose module(s) from any routine that calls any of the procedures. This will enable the compiler to check argument consistency at compile time.

Fortran 中致命内存错误的最常见原因是非法数组下标以及调用过程(子例程或函数)的参数与过程声明的参数之间的不一致。首先,打开运行时下标检查。使用 gfortran-fcheck=bounds或更好的,打开额外的运行时检查-fcheck=all。对于过程参数问题,将所有过程放入模块和use调用任何过程的任何例程中的那些模块。这将使编译器能够在编译时检查参数一致性。

回答by Matheus Danella

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”上!因为当我编译它时将“naxes”替换为“2”,例如,它不会给我结果(当然)但错误消失了!

Does someone can maybe tell me why is this error with "naxes" happening?? Thank you all again!!!

有人可以告诉我为什么会发生“naxes”错误?再次感谢大家!!!