0

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
GPSmaster
  • 844
  • 3
  • 15
  • 31
  • You've misunderstood the advice offered in the answer to the duplicate. Put the subroutine into a module and let the compiler create the interface for you. If I have time later I'll find an example hereabouts for your education. – High Performance Mark Jul 12 '16 at 13:40
  • @HighPerformanceMark I'm interested in seeing an example, if you have time at some point. Thanks! – GPSmaster Jul 14 '16 at 18:34

0 Answers0