記一個(gè)gcc7代編譯器下allocate-source中有關(guān)array bounds的離譜bug

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è)字, 離譜!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容