fortran的另一个好的分段错误

时间:2016-02-14 09:06:45

标签: fortran gfortran

我知道人们在这里问了很多分段错误的问题,但是我已经把我的努力解决了这个问题超过三个小时,但仍然无法解决这个问题。 :/所以这是我的代码:

c     sinle event analysis

  implicit real(a-h,o-z)
  real day(12), nmonth(12), year(12), clas(12),
 $ hour(12), nmin(12)
  integer mark(12)
  real tst(12), D(12), avgP(12,6), avgA(12,6)
  integer k, m, n, g

  real time(2054904), proa(2054904), prob(2054904), w1(2054904),
 $ w2(2054904), w3(2054904), w4(2054904) 

  D(1) = 31; D(2) = 28; D(3) = 31; D(4) = 30; D(5) = 31;
  D(6) = 30; D(7) = 31; D(8) = 31; D(9) = 30; D(10) = 31;
  D(11) = 30; D(12) = 31

  open(100,file='singleE.dat')

  do i=1, 12
   tst(i)=0
  enddo

900我= 1,12        读(100,1150)天(i),nmonth(i),年(i),      $ hour(i),nmin(i),clas(i)

   do j=1, 12
    if (int(nmonth(i)).EQ.(13-j)) then
    tst(i) = tst(i) + D(12-j)
    nmonth(i) = nmonth(i)-1
    endif
   enddo

   tst(i) = tst(i) + day(i) + (year(i) - 2010)*365
 $  + (hour(i) + nmin(i)/60)/24 

   if (year(i) > real(2011)) then 
    tst(i) = tst(i) + 1
   endif
  enddo

  open(200,file='hole.dat',status='OLD')

  k = 0

  do i=1, 2054904
   read(200,950) time(i), proa(i), prob(i),
 $ w1(i), w2(i), w3(i), w4(i) 
  enddo

  mark = 0

  do i=1, 12
   do j=1, 2054904
    k = k + 1
    if(abs(tst(i)-time(j))<0.0001) then
     mark(i) = k
    endif
   enddo
  enddo      

  n = 5;

  do i= 1, 12
   do j= 1,6
    avgP(i,j) = 0
    avgA(i,j) = 0
   enddo
  enddo



  do i=1, 12
   if (mark(i).EQ.0) then
    go to 750
   endif

   do j = (mark(i)-(n+1)*1440), (mark(i)-n*1440)
    avgP(i,1) = avgP(i,1) + proa(j) 
    avgP(i,2) = avgP(i,2) + prob(j)
    avgP(i,3) = avgP(i,3) + w1(j)
    avgP(i,4) = avgP(i,4) + w2(j)
    avgP(i,5) = avgP(i,5) + w3(j)
    avgP(i,6) = avgP(i,6) + w4(j)
   enddo

   do g = (mark(i)+n*1440), (mark(i)+(n+1)*1440)
    avgA(i,1) = avgA(i,1) + proa(g)
    avgA(i,2) = avgA(i,2) + prob(g)
    avgA(i,3) = avgA(i,3) + w1(g)
    avgA(i,4) = avgA(i,4) + w2(g)
    avgA(i,5) = avgA(i,5) + w3(g)
    avgA(i,6) = avgA(i,6) + w4(g)
   enddo

750 print *,avgP(i,1),avgP(i,2),avgP(i,3),avgP(i,4),      $ avgP(i,5),avgP(i,6)

  enddo

850 close(i)

950格式(F12.7,2x,E10.3,2x,E10.3,2x,E10.3,2x,E10.3,      $ 2x,E10.3,2x,E10.3)

1150格式(F2.0,1x,F2.0,1x,F4.0,1x,F2.0,1x,F2.0,4x F3.1)

  end

导致我麻烦的部分是循环:

  do i=1, 12
   if (mark(i).EQ.0) then
    go to 750
   endif

   do j = (mark(i)-(n+1)*1440), (mark(i)-n*1440)
    avgP(i,1) = avgP(i,1) + proa(j) 
    avgP(i,2) = avgP(i,2) + prob(j)
    avgP(i,3) = avgP(i,3) + w1(j)
    avgP(i,4) = avgP(i,4) + w2(j)
    avgP(i,5) = avgP(i,5) + w3(j)
    avgP(i,6) = avgP(i,6) + w4(j)
   enddo

   do g = (mark(i)+n*1440), (mark(i)+(n+1)*1440)
    avgA(i,1) = avgA(i,1) + proa(g)
    avgA(i,2) = avgA(i,2) + prob(g)
    avgA(i,3) = avgA(i,3) + w1(g)
    avgA(i,4) = avgA(i,4) + w2(g)
    avgA(i,5) = avgA(i,5) + w3(g)
    avgA(i,6) = avgA(i,6) + w4(g)
   enddo


  enddo

使用gdb,我发现'j'循环导致了问题。所有参数都很好,但每次执行程序时,'j​​'循环只进行一次。特殊的是,随着'i'的增加,流程开始逐一削弱。例如,在i = 1时,循环执行得很好。然后,在i = 2时,avgP(i,6)= avgP(i,6)+ w4(j)导致seg故障。在i = 3时,avgP(i,5)= avgP(i,5)+ w3(j)导致seg故障,并且最后在i = 7时,整个循环不起作用。多么奇怪的错误!希望我能得到一些帮助。

1 个答案:

答案 0 :(得分:1)

循环

do j=1, 12 if (int(nmonth(i)).EQ.(13-j)) then tst(i) = tst(i) + D(12-j) nmonth(i) = nmonth(i)-1 endif enddo 可能会在D(0)时尝试访问值j=12,但D的尺寸为1:12,以便知道写入tst的内容及其后果。

这应该通过启用边界检查来捕获。