这是我使用 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
我很困惑为什么在子例程theArray
中不可分配,arraySub
因为我用intent(inout)
and初始化它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