Community
cancel
Showing results for 
Search instead for 
Did you mean: 
bucaioni__thomas
New Contributor I
97 Views

[allocate] Dynamic array indices

Jump to solution

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

Accepted Solutions
Arjen_Markus
Valued Contributor III
91 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
Valued Contributor III
92 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

bucaioni__thomas
New Contributor I
85 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

Reply