I have the following structure:
PROGRAM myPro USE mMod1 IMPLICIT NONE REAL t INTEGER i CALL proc1 ! do something... ! I can access here dt defined in mMod1 ! but not myArr1 END PROGRAM myPro
MODULE mMod1 IMPLICIT NONE REAL dt REAL, ALLOCATABLE :: myArr1(:,:) CONTAINS SUBROUTINE proc1 USE mMod2 CHARACTER(LEN=256) fn CALL proc2(fn,myArr1) ! I can access here both dt and myArr1 END SUBROUTINE proc1 END MODULE mMod1
MODULE mMod2 IMPLICIT NONE CONTAINS SUBROUTINE proc2(fn,myArr1) CHARACTER(LEN=256), INTENT(IN) :: fn REAL, ALLOCATABLE :: myArr1(:,:) INTEGER n INTEGER, PARAMETER :: dim = 3 ! read some input file and determine n ALLOCATE (myArr1(dim,n)) ! fill in the array by reading the rest of the file END SUBROUTINE proc2 END MODULE mMod2
The question is, the definition of both dt and myArr is the same except that myArr is an allocatable array allocated in a separate module. But I cannot access myArr1 in the main program while I can access dt in the main program. Why? And how to make changes so that I can access it there.
The whole purpose is: I want to read an input file in a separate subroutine into an array (myArr1) and have this array available globaly. This is why I declare it in mMod1. The dimensions of the array are written at the top of the file so I do not know them until I have started reading the file. So the allocation of the myArr1 has to happen inside the separate procedure, I would include it as a separate procedure but it does not work as probably INTERFACE is necessary, so I decided to wrap the procedure in a separate module.
It should work the example below runs fine.
module fred implicit none real, allocatable :: arry(:,:) contains subroutine bill() integer :: istat allocate(arry(3,3), stat = istat) arry = 0.0 end subroutine bill end module fred program Console3 use fred implicit none integer :: istat if (allocated( arry ) ) then arry=1.0 arry(1,2) = 2.0 else allocate(arry(3,3), stat = istat) arry = 0.0 arry(3,3) = 3.0 end if print *, 'Hello World' print *, arry end program Console3