Bug情況
最近寫Fortran代碼的時(shí)候遇到了個(gè)怪bug, 代碼如下:
然后離譜的來了, 我發(fā)現(xiàn)在gcc750和gcc710里面跑出來的shape_data的lbound都是0, 也就是說index是從0開始的, 像C一樣了...
但是在gcc11下面就沒問題, 是從1開始的
而且下面注釋那幾行打開的話也沒問題, 似乎就是傳參進(jìn)去才會(huì)有問題
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
解決思路
- 要么換個(gè)編譯器
- 要么像上面注釋行一樣, 制定以下shape吧..但是這樣用source的意義小了...就是少寫一行吧那
總結(jié), 一個(gè)字, 離譜!