问题
I'm trying to use an allocatable array in a subroutine but the compiler complains that
Error: Dummy argument 'locs' with INTENT(IN) in variable definition context (ALLOCATE object) at (1)
The only thing I could find was that I am supposed to use an explicit interface, which I am doing. Here the relevant code for the subroutine:
RECURSIVE SUBROUTINE together(locs, LL, RL)
INTEGER, DIMENSION(:,:), ALLOCATABLE, INTENT(IN) :: locs
INTEGER, INTENT(IN) :: LL, RL
ALLOCATE(locs(LL,RL))
END SUBROUTINE together
回答1:
The compiler's error message is one descriptive of the problem. With INTENT(IN)
you are saying that the object will not change, but you then go on to attempt to ALLOCATE
it.
Yes, an explicit interface will be required for the calling, but that isn't the problem.
The Fortran 2008 standard says in section 5.3.10 that
A nonpointer object with the INTENT (IN) attribute shall not appear in a variable denition context
Allocation is one such context: section 16.6.7, point (11).
回答2:
The locs
dummy argument is allocatable, and has the INTENT(IN) attribute - the intent attribute here indicating that the calling procedure is providing information to the subroutine.
A consequence of the INTENT(IN) attribute is that you cannot change the allocation status (or value) of locs
. Your ALLOCATE statement is attempting to do just that.
回答3:
Try allocating your array in your main program, then when locs
is pushed to your subroutine, use INTENT(INOUT)
to tell the compiler you also want to change the contents of your array.
来源:https://stackoverflow.com/questions/22169365/fortran-allocatable-array-in-subroutine