Here is some fortran90 code that I'm compiling using the PGI fortran compiler. I can't seem to allocate the array within the subroutine arraySub()
without seg faulting. It also seg faults if I even check if the array is allocated like this: allocated(theArray)
. I'm compiling the code on the command line like this: 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
I'm confused as to why theArray
is not allocatable inside the arraySub
subroutine since I'm initializing it with intent(inout)
and allocatable
. Especially strange is that allocated(theArray)
also seg faults. I was wondering if someone could point me in the right direction to a solution. Thanks in advance!
Edit: Since there isn't actual example code in the duplicate question, I'll post a solution using a module:
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