简体   繁体   中英

“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. 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.

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)

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:

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.

And here is the full program, I am using "gfortran Codigo.f90 -o TESTE -Lcfitsio -lcfitsio" to compile it:

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!

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 use those module(s) from any routine that calls any of the procedures. 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:

At line 65 of file Codigo.f90 Fortran runtime error: Index '0' of dimension 1 of array 'l' below lower bound of 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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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