Fortran: Activated Variables in Modules
I want to use the Ar (-3: 3, 5) array, which is an allocable variable in a global module and allocates it in one subroutine and accesses it in the next subroutine (see code snippets below). Will there be indexing in the second subroutine from -3 to 3 and 1 to 5, or do I need to specify what's in the subroutine?
module global
real, allocatable(:,:) :: Ar
end module global
subroutine allocateAr
use global
ALLOCATE(Ar(-3:3, 5))
end subroutine allocateAr
subroutine useAr
use global
Ar(-3,1)=3.0 !is this -3,1 here or do I have to use 1,1????
end subroutine useAr
source to share
Attribute arrays always retain their bounds if you refer to them as allocatables. It even means using 'use association' or 'host association' directly, as you show in a subroutine useAR
, or if you pass them as put arguments. If you pass them as intended shape or intended size arrays, you must specify lower bounds in each procedure called, otherwise it will default to 1.
So, in your case, you can use -3,1
.
Otherwise, I agree with Jonathan Dursi regarding globally mutable state.
source to share