Is there any caveat in using custom indices for allocatables? For example
real, dimension (:,:), allocatable :: T
allocate ( T(0:2,5:7) )
T = 0
T(0,5) = 1
T(2,7) = 9
print *, T
works great, but it would be to be sure there is no side effects
Link Copied
The one caveat I can think of is that if you pass the array to a function or subroutine (including intrinsic ones!) the custom indices are not automatically passed on (for good reasons in Here is a small demonstration:
! find_elem.f90 --
! Demonstrate (minor) problem with custom lower-bound
!
program find_elem
implicit none
integer, dimension(-3:3) :: value
integer, dimension(7) :: basic
value = 0
value(1) = 1 ! Note: fifth element in the array
basic = value ! Copy, starts at start of the array
write(*,*) findloc(value, 1), findloc(basic,1)
end program find_elem
The output is NOT "1 5", but "5 5" - the lower bound is not passed to the intrinsic findloc. If you want to use no-default lower/upper bounds in your own routines, you have to explicit use them in the declaration.
The one caveat I can think of is that if you pass the array to a function or subroutine (including intrinsic ones!) the custom indices are not automatically passed on (for good reasons in Here is a small demonstration:
! find_elem.f90 --
! Demonstrate (minor) problem with custom lower-bound
!
program find_elem
implicit none
integer, dimension(-3:3) :: value
integer, dimension(7) :: basic
value = 0
value(1) = 1 ! Note: fifth element in the array
basic = value ! Copy, starts at start of the array
write(*,*) findloc(value, 1), findloc(basic,1)
end program find_elem
The output is NOT "1 5", but "5 5" - the lower bound is not passed to the intrinsic findloc. If you want to use no-default lower/upper bounds in your own routines, you have to explicit use them in the declaration.
For my use case, it should be ok then. But it's good to know functions and subroutines need to be more cautious.
Thank you
For more complete information about compiler optimizations, see our Optimization Notice.