billgray1234
Programmer
- Mar 14, 2011
- 39
i'm using fortran 90/95.
i'm using recursive subroutines.
i need to use arrays within these subroutines. i'm trying to determine whether i can use allocatable arrays, or whether i instead have to use "fixed size" arrays.
as you probably know, when using allocatable arrays, if they have already been allocated (i.e. they are currently in use), then they have to be deallocated first, if they are to be allocated again (i.e. used again).
does this also hold true if :-
1) the allocatable array is used in a recursive subroutine, and
2) the allocatable array is not a part of the subroutine dummy arguement list, and
AFTER the array has been allocated, but BEFORE the array has been deallocated, either :-
3) the recursive subroutine calls itself, or
4) control is "returned" to the calling program (using the word RETURN) ?
for example, is the code below valid, or does the array A have to instead be declared as a "fixed size" array (i.e. have a fixed dimension) ? my guess is that A has to instead be declared as a "fixed size" array.
RECURSIVE SUBROUTINE F ( X1 , X2 )
INTEGER, INTENT (INOUT) :: X1
INTEGER, INTENT (INOUT) :: X2
INTEGER :: IA
REAL, ALLOCATABLE, DIMENSION ) :: A
IA = 3, say
ALLOCATE ( A (IA) )
IF ( X1 < 5 ) THEN
...X1 = X1 + 1
...CALL F ( X1 , X2 )
ELSE
...perform some "final" calculation with X2, and A
...RETURN
END IF
DEALLOCATE ( A )
END SUBROUTINE F
i'm using recursive subroutines.
i need to use arrays within these subroutines. i'm trying to determine whether i can use allocatable arrays, or whether i instead have to use "fixed size" arrays.
as you probably know, when using allocatable arrays, if they have already been allocated (i.e. they are currently in use), then they have to be deallocated first, if they are to be allocated again (i.e. used again).
does this also hold true if :-
1) the allocatable array is used in a recursive subroutine, and
2) the allocatable array is not a part of the subroutine dummy arguement list, and
AFTER the array has been allocated, but BEFORE the array has been deallocated, either :-
3) the recursive subroutine calls itself, or
4) control is "returned" to the calling program (using the word RETURN) ?
for example, is the code below valid, or does the array A have to instead be declared as a "fixed size" array (i.e. have a fixed dimension) ? my guess is that A has to instead be declared as a "fixed size" array.
RECURSIVE SUBROUTINE F ( X1 , X2 )
INTEGER, INTENT (INOUT) :: X1
INTEGER, INTENT (INOUT) :: X2
INTEGER :: IA
REAL, ALLOCATABLE, DIMENSION ) :: A
IA = 3, say
ALLOCATE ( A (IA) )
IF ( X1 < 5 ) THEN
...X1 = X1 + 1
...CALL F ( X1 , X2 )
ELSE
...perform some "final" calculation with X2, and A
...RETURN
END IF
DEALLOCATE ( A )
END SUBROUTINE F