2

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
user1638145
  • 1,979
  • 2
  • 14
  • 14
  • I would urge you not to use global variables. This is just one of many unnecessary questions you will face that would be largely bypassed if you simply pass the variables between routines rather than rely on global mutable state. – Jonathan Dursi Apr 29 '13 at 12:47
  • 1
    May be a good idea if I'd write this program from scratch, but I will not change several thousand lines of code... – user1638145 Apr 29 '13 at 13:12
  • The time you spend updating a few thousand lines of code will be much less than the time spent repeatedly debugging the same few thousand lines of code unnecessarily; especially if you use modern tools like photran in eclipse to do the refactoring. – Jonathan Dursi Apr 29 '13 at 13:22
  • Maybe I should learn some programming and find out what phortran and refactoring is. – user1638145 Apr 29 '13 at 13:26

1 Answers1

2

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.