Fortran Coder

楼主: wxy
打印 上一主题 下一主题

[求助] 动态数组内存问题

[复制链接]

67

帖子

16

主题

0

精华

专家

F 币
275 元
贡献
201 点
跳转到指定楼层
楼主
发表于 2017-9-15 17:51:49 | 显示全部楼层 |只看大图 回帖奖励 |倒序浏览 |阅读模式
运行到24行时出现内存错误提示,不明白什么原因
[Fortran] 纯文本查看 复制代码
Program weather
  Implicit None

  Integer l, i, j, m, daysinyear, k, mm, e, z
  Integer, Allocatable :: site1(:), site2(:), year(:), month(:), day(:)

  Real, Allocatable :: lat(:), lon(:), h(:), tmax(:), tmin(:), lonx(:), laty(:)
  Integer :: ave, ier
  Real :: h2

  Character (45) :: filename = 'SURF_CLI_CHN_MUL_DAY-TEM-12001-123456.txt'
  Character (8) :: outfile = '12345678'
  Character (Len=20) :: line

!=====================================
  Open (5, File='D:\气象数据New\经纬度高程\经纬度高程.txt')
  e = 2421
  Allocate (site2(e), lonx(e), laty(e))
  Do z = 1, e
    Read (5, *) site2(z), lonx(z), laty(z), h2
    write(*,*) site2(z), lonx(z), laty(z), h2
  End Do
  Deallocate (site2, lonx, laty)
  Close (5)
  Do l = 2010, 2015
    Do j = 1, 12
      Write (filename(32:37), '(i6)') l*100 + j
      Open (Unit=11, File='D:\气象数据\气温\'//filename, Action='READ')

      m = 0
      Do
        Read (11, *, Iostat=ier) line
        If (ier==-1) Exit ! end-of-file
        m = m + 1
      End Do
      Allocate (site1(m), lat(m), lon(m), h(m), year(m), month(m), day(m), tmax(m), tmin(m))

      Rewind (11)
      mm = 0
      Do i = 1, m
        mm = mm + 1
        Read (11, *) site1(mm), lat(mm), lon(mm), h(mm), year(mm), month(mm), day(mm), ave, tmax(mm), tmin(mm)
        lat(mm) = lat(mm)/100
        lon(mm) = lon(mm)/100

        tmax(mm) = tmax(mm)*0.1
        tmin(mm) = tmin(mm)*0.1
        If (h(mm)>=100000) Then
          h(mm) = (h(mm)-100000)
        End If
        h(mm) = h(mm)*0.1
        If (tmax(mm)==3270.0 .Or. tmax(mm)==3276.6 .Or. tmin(mm)==3270.0 .Or. tmin(mm)==3276.6) mm = mm - 1

      End Do
      Close (Unit=11)

      Do k = 1, daysinyear(l, j)
        Write (outfile(1:8), '(i8)') l*10000 + j*100 + k
        Open (Unit=15, File='D:\气象数据\tmax\'//outfile//'.txt')
        Open (Unit=16, File='D:\气象数据\tmin\'//outfile//'.txt')
        Do i = 1, mm

          If (l==year(i) .And. j==month(i) .And. k==day(i)) Then


            Do While (site1(i)==site2(z))
              Write (15, '(i5,2x,f6.2,2x,f5.2,2x,f12.4,2x,f12.4,2x,f6.2)') &
              &site1(i), lon(i), lat(i), lonx(z), laty(z), tmax(i) + (h(i)/100)*0.6
! "(i5,2x,f5.2,2x,f6.2,2x,f5.0,2x,i5,2x,i2,2x,i2,2x,f6.2)"
              Write (16, '(i5,2x,f6.2,2x,f5.2,2x,f12.4,2x,f12.4,2x,f6.2)') &
              &site1(i), lon(i), lat(i), lonx(z), laty(z), tmin(i) + (h(i)/100)*0.6
              Print *, site1(i), year(i), month(i), day(i)

            End Do

          End If
        End Do
        Close (Unit=15)
      End Do

      Deallocate (site1, lat, lon, h, year, month, day, tmax, tmin)
    End Do
  End Do

End Program weather

!=======================================

Integer Function daysinyear(year, month)
  Integer, Intent (In) :: year, month

  Integer :: daysinmonth(12) = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]

  If (((mod(year,4)==0) .And. (mod(year,100)/=0)) .Or. (mod(year,400)==0)) Then
    daysinmonth(2) = 29
  Else
    daysinmonth(2) = 28
  End If

  daysinyear = daysinmonth(month)
End Function daysinyear

QQ截图20170915175358.png (39.82 KB, 下载次数: 447)

QQ截图20170915175358.png
分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

67

帖子

16

主题

0

精华

专家

F 币
275 元
贡献
201 点
沙发
 楼主| 发表于 2017-9-16 00:15:03 | 显示全部楼层
本帖最后由 wxy 于 2017-9-16 00:29 编辑
chiangtp 发表于 2017-9-15 21:31
line24: CLOSE(UNIT=5)?
不要用 UNIT= 0, 5, 6 對應到 diskfile (尤其是又有 READ/WRITE(*,...)時, compile ...

额 我换成其他10,13等还是同样错误,我在运行时,到第24行运行都没问题,是16-24行这部分循环运行结束时报错,我想是不是后面67行这儿使用之前read内容时出错了

67

帖子

16

主题

0

精华

专家

F 币
275 元
贡献
201 点
板凳
 楼主| 发表于 2017-9-16 13:11:58 | 显示全部楼层
本帖最后由 wxy 于 2017-9-16 13:21 编辑
chiangtp 发表于 2017-9-16 11:38
1. 還是有點小錯:  "k==day(i)"  ---> "k/=day(i)"
2. 改善Loop的效率: "IF(l/=year(i) .OR. j/=month(i) ...

不好意思刚看到 非常感谢 ,我还是不太明白这个报错是什么意思,为甚末之前将deallocate放在下一个do循环之前就会报错,放在end program 之前就没事,deallocate不是将之前的动态数组内存释放掉吗,2. '/=,.or.'和‘==,.and.’这里有点转不过来,'/='不是‘不等于‘’吗

67

帖子

16

主题

0

精华

专家

F 币
275 元
贡献
201 点
地板
 楼主| 发表于 2017-9-16 16:44:24 | 显示全部楼层
chiangtp 发表于 2017-9-16 13:57
(1) 为甚末之前将deallocate放在下一个do循环之前就会报错,放在end program 之前就没事
DEALLOCATE(site ...

嗯嗯 明白了 非常感谢
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-3 18:53

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表