美文网首页
记一个gcc7代编译器下allocate-source中有关ar

记一个gcc7代编译器下allocate-source中有关ar

作者: 别有路 | 来源:发表于2023-07-19 10:43 被阅读0次

Bug情况

最近写Fortran代码的时候遇到了个怪bug, 代码如下:

然后离谱的来了, 我发现在gcc750和gcc710里面跑出来的shape_data的lbound都是0, 也就是说index是从0开始的, 像C一样了...

但是在gcc11下面就没问题, 是从1开始的

而且下面注释那几行打开的话也没问题, 似乎就是传参进去才会有问题


module m1
  implicit none

contains
  subroutine m1s1(data)
    implicit none
    integer(kind=4),intent(in) :: data(:)

    integer, allocatable :: shape_data(:)

    allocate(shape_data, source=shape(data))
    ! allocate(shape_data(size(shape(data))), source=shape(data))   ! it's ok
    print *, ubound(shape_data), ubound(shape(data))
    print *, lbound(shape_data), lbound(shape(data))

  end subroutine
end module


program main
  use m1
  implicit none
  integer,allocatable :: a(:)


  call m1s1([1,2,3])
  
  !allocate(a, source=shape([4,4,5]))
  !print *, ubound(a)
  !print *, lbound(a)



end program

run一下:

DESKTOP-TKTNVE0:shape_bound$ gfortran --version
GNU Fortran (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0

DESKTOP-TKTNVE0:shape_bound$ gfortran main.F90
DESKTOP-TKTNVE0:shape_bound$ ./a.out
           0           1
           0           1

解决思路

  • 要么换个编译器
  • 要么像上面注释行一样, 制定以下shape吧..但是这样用source的意义小了...就是少写一行吧那

总结, 一个字, 离谱!

相关文章

网友评论

      本文标题:记一个gcc7代编译器下allocate-source中有关ar

      本文链接:https://www.haomeiwen.com/subject/iuioudtx.html