user1638145
user1638145

Reputation: 2049

Fortran: Allocatable variables in modules

I want to use an array Ar(-3:3, 5), which is an allocatable variable in the module global and allocate it in one subroutine and access it in the next subroutine (see code snippets below). Will the indexing in the second subroutine be from -3 to 3 and from 1 to 5, or do I have to specify that 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

Upvotes: 2

Views: 2003

Answers (1)

Allocatable arrays always retain their bounds, if you access them as allocatables. This means even directly using 'use association' or 'host association', as you show in subroutine useAR, or if you pass them as allocatable dummy arguments. If you pass them as assumed shape or assumed size arrays, you must specify the lower bounds in every called procedure, otherwise it will default to 1.

So in your case, you can use -3,1.

Otherwise I agree with Jonathan Dursi regarding the global mutable state.

Upvotes: 2

Related Questions