これは、PGI fortran コンパイラを使用してコンパイルしている fortran90 コードです。セグフォルトなしでは、サブルーチン内で配列を割り当てることができないようですarraySub()
。配列が次のように割り当てられているかどうかを確認しても、エラーが発生しますallocated(theArray)
。次のようにコマンドラインでコードをコンパイルしています。pgf90 -o test.exe test.f90
program test
implicit none
real,dimension(:,:),allocatable :: array
write(*,*) allocated(array) ! Should be and is false
call arraySub(array)
end program
subroutine arraySub(theArray)
implicit none
real,dimension(:,:),allocatable,intent(inout) :: theArray
write(*,*) 'Inside arraySub()'
allocate(theArray(3,2)) ! Seg fault happens here
write(*,*) allocated(theArray)
end subroutine
and で初期化しているため、サブルーチンtheArray
内で割り当てられない理由について混乱しています。特に奇妙なのは、セグ フォールトもあるということです。誰かが私を正しい方向に向けて解決できるかどうか疑問に思っていました。前もって感謝します!arraySub
intent(inout)
allocatable
allocated(theArray)
編集:重複した質問には実際のサンプルコードがないため、モジュールを使用してソリューションを投稿します:
module arrayMod
real,dimension(:,:),allocatable :: theArray
end module arrayMod
program test
use arrayMod
implicit none
interface
subroutine arraySub
end subroutine arraySub
end interface
write(*,*) allocated(theArray)
call arraySub
write(*,*) allocated(theArray)
end program test
subroutine arraySub
use arrayMod
write(*,*) 'Inside arraySub()'
allocate(theArray(3,2))
end subroutine arraySub