Hi,
I'm using (and at the same time trying to master) Fortran 95...
Okay, suppose that I have an array,
and I want to pass this to a subroutine WHILE keeping the "non-default" indexing (because the indexing contains important information). The ususal assumed-shape array passing doesn't seem to work the way I want;
writes
... So the subroutine seems to forget about it's initial lower and upper indexes and automatically uses the default lower index 1. Okay, I could use an explicit-shape approach like this:
which gives the desired result
but at the cost of having to explicitly pass more parameters (which can be quite cumbersome when one have to pass several arrays to a subroutine).
I have discovered that if the ASSUMED subroutine is modified to arrays declared ALLOCATABLE I do get the desired result:
gives
My questions are then:
* Are there some "assumed-array way" of passing the indexes to the subroutine without using allocatable arrays?
* Why does it work with allocatable arrays and not ordinary ones?
* Is there some (performance or otherwise) penalty when using allocatable arrays instead of ordinary ones?
I would be grateful if anyone could help me out here;-)
I'm using (and at the same time trying to master) Fortran 95...
Okay, suppose that I have an array,
Code:
INTEGER, DIMENSION(-5:5) :: arr
and I want to pass this to a subroutine WHILE keeping the "non-default" indexing (because the indexing contains important information). The ususal assumed-shape array passing doesn't seem to work the way I want;
Code:
SUBROUTINE ASSUMED(a)
INTEGER, DIMENSION(:) :: a
WRITE(*,*) lbound(a), ubound(a)
END SUBROUTINE
CALL ASSUMED(arr)
Code:
1 11
Code:
SUBROUTINE EXPLICIT(a, l, u)
INTEGER :: l, u
INTEGER, DIMENSION(l:u) :: a
WRITE(*,*) lbound(a), ubound(a)
END SUBROUTINE
CALL EXPLICIT(arr, -5, 5)
Code:
-5 5
I have discovered that if the ASSUMED subroutine is modified to arrays declared ALLOCATABLE I do get the desired result:
Code:
INTEGER, DIMENSION(:), ALLOCATABLE :: arr2
ALLOCATE(arr2(-5:5)
SUBROUTINE ASSUMED2(a)
INTEGER, DIMENSION(:), ALLOCATABLE :: a
WRITE(*,*) lbound(a), ubound(a)
END SUBROUTINE
CALL ASSUMED2(arr2)
Code:
-5 5
* Are there some "assumed-array way" of passing the indexes to the subroutine without using allocatable arrays?
* Why does it work with allocatable arrays and not ordinary ones?
* Is there some (performance or otherwise) penalty when using allocatable arrays instead of ordinary ones?
I would be grateful if anyone could help me out here;-)