Passing Allocatable arrays into Subroutine outside Argument list
Passing Allocatable arrays into Subroutine outside Argument list
(OP)
I am using Compaq Visual Fortran.
I need to pass an allocatable array into a subroutine. The trick is that I can not simply add it to the argument list because the subroutine is called from an IMSL routine which permits only 3 specific variables in the argument list. I don't think I can use a COMMON statement with allocatable arrays.
The only solution I have come up with is to 'over-define' the array rather than making it allocatable but this isn't very appealing. Any other ideas?
I need to pass an allocatable array into a subroutine. The trick is that I can not simply add it to the argument list because the subroutine is called from an IMSL routine which permits only 3 specific variables in the argument list. I don't think I can use a COMMON statement with allocatable arrays.
The only solution I have come up with is to 'over-define' the array rather than making it allocatable but this isn't very appealing. Any other ideas?
RE: Passing Allocatable arrays into Subroutine outside Argument list
CALL SUB1( ARG1, ARG2, XARRAY )
:
:
SUBROUTINE SUB1( ARG1, ARG2, XARRAY )
DIMENSION XARRAY(1)
:
:
RETURN
END
This should compile and run fine. Potential issues are:
1) You have to make sure you don't blow past the end of the array. The best thing to do here is put some type of marker in the last element of the array and test for it.
2) You won't be able to debug the array contents, since the compiler doesn't know how big the array is with the above dimensioning technique.
Richard Ay
COADE, Inc.
RE: Passing Allocatable arrays into Subroutine outside Argument list
Now... if the array you want to pass to the sub has been allocated... you can pass it to the sub in the argument list. Of course... you say that the IMSL routine which calls the sub only has 3 arguments. I understand that. Also... you are correct that you can't place an allocated array in Common. Here is another solution for you.
I assume you are using an F90 or F95 compiler ( since you are working with allocatable arrays ). Thus... you have the ability to make "generic" subroutines which use the same name. Essentially, you make two subroutines with slightly different names. Then you use a "Module Procedure" statement to make those subs generic.
Module test
Interface Generic_Sub
Module Procedure Sub_1, Sub_2
End Interface Generic_Sub
Contains
Sub_1( x, y, z )
Integer :: x, y, z
::::::::
End Sub_1
Sub_2( x, y, z, a )
Integer :: x, y, z, a(*)
::::::::
End Sub_1
End Module test
Main Program
USE test, Only: Generic_Sub
:::::
Call Generic_Sub ( x, y, z ) ! IMSL callable
Call Generic_Sub ( x, y, z, a ) ! as You need it
End Program Main
Dan
www.dtware.com