Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28435 Discussions

[allocate] Dynamic array indices

bucaioni__thomas
New Contributor I
962 Views

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

 

0 Kudos
1 Solution
Arjen_Markus
Honored Contributor I
956 Views

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.

View solution in original post

2 Replies
Arjen_Markus
Honored Contributor I
957 Views

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.

bucaioni__thomas
New Contributor I
950 Views

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

0 Kudos
Reply