Fortran使いの皆様こんにちは。最近、Intel One API 2025からifortがなくなり、ifxを使う必要が出てきました。
一つセグメンテーションフォルトが起きる事例がありましたので、その回避策とともに紹介します。
integer, optional, intent(in) :: rec
というoptional引数がある時、
if (present(rec) .and. (rec > 0)) then
irec = rec - 1
else
irec = 0
end if
は落ちます。これは、rec > 0
を評価しようとして、recが定義されていないからのようです。ifortだと、左を評価してから右を評価してるっぽくて、特に問題はなかったようですが...
if (present(rec)) then
if (rec > 0) then
irec = rec - 1
end if
else
irec = 0
end if
のようにすると回避できます。
2025/4/9追記
最小構成を作って
module testsub
contains
subroutine test(k,rec)
implicit none
integer,intent(in)::k
integer,intent(in),optional::rec
integer::irec
if (present(rec) .and. (rec > 0)) then
irec = rec - 1
else
irec = 0
end if
end subroutine test
end module testsub
program main
use testsub
implicit none
integer::k
k = 3
call test(k)
end program
色々試していますが、ifortやifxではなく、むしろgfortranで落ちているかもしれません。
gfortran 11だと上のコードがセグフォになることを確認しました。もう少し再現環境について調べます。