0

I have some allocatable array which their size is determined in the subroutine. How can I introduce them in the main block which I lately not have any problems with it? Symbolic code is:

Main PROG

IMPLICIT NONE

REAL, DIMENSION(:,:), ALLOCATABLE :: A
REAL, DIMENSION(:,:), ALLOCATABLE :: B
REAL, DIMENSION(:,:), ALLOCATABLE :: C

CALL SUB(A,B,C)

!Continue the program

END MAIN PROG
-------------------
SUB(A,B,C)

IMPLICIT NONE

REAL, DIMENSION(:,:) :: A
REAL, DIMENSION(:,:) :: B
REAL, DIMENSION(:,:) :: C

!OPERATION ON A AND B WHICH DETERMINE THE DIMENSION OF C

END SUB

I also read these:

Can a input argument of a Fortran subroutine be deallocated and allocated in the body of the subroutine?

Array allocation in Fortran subroutine

But, none of them is practical in my case.


I consider them as below:

ALLOCATE/DEALLOCATE solution:

If a variable allocated in the main then you have two choices:

  1. None introduce it in the variable declaration and deallocate it then you have the error: The variable is not allocatable or dummy

  2. Introduce it in the variable declaration and deallocate it then you have the error: The variable is a dummy argument so cannot allocatable.

INTERFACE solution:

The main application of it in the module if you don't have a module or the variable read and allocate in subroutine it doesn't work.

Vladimir F solution:

His solution has two limitations. It works in case you read the variable in the subroutine. This solution is workable with the module because you need to add a subroutine just for reading the variable.


If my argument has a fault, or you have another solution, or you have a description to mention the solution to be applicable please note me.

VOLCANIC_9
  • 53
  • 7
  • There are 2 options for each variable `a,b,c`: 1) allocate before `call sub`. 2) mark the dummy variable in the definition of `sub` as `allocatable` s.t. you can allocate it within the subroutine. – jack Jan 25 '21 at 09:13
  • Is that what you were asking about? – jack Jan 25 '21 at 09:14
  • It's not clear which part you are finding difficult, but an answer to the linked question covers several important points: allocatable dummy argument, intent, explicit interface. If those don't cover what you need to know, please clarify the question. – francescalus Jan 25 '21 at 09:19
  • @jack thanks for your comment. I have no prediction about size range variation so I think pre-allocate is not sufficient. I cant allocate variables that intent in or out from subroutine ( if I properly understand your comment). – VOLCANIC_9 Jan 25 '21 at 09:21
  • @francescalus I add more details to the question. – VOLCANIC_9 Jan 25 '21 at 10:14
  • Could you explain why `real, allocatable, dimension(:,:), intent(out) :: A, B, C` doesn't work for you in the subroutine, followed by an allocate in the subroutine once you've determined the appropriate sizes? (This will need an explicit interface for `sub`.) – francescalus Jan 25 '21 at 11:16
  • @francescalus The compiler shows an error: The variable is a dummy argument so cannot allocatable. – VOLCANIC_9 Jan 25 '21 at 12:57
  • 1
    If you can't update to a compiler released this century then you are going to find programming in Fortran a very painful experience. Which Fortran 90/95 compiler are you using (including release version)? Someone may be able to advise you how to write Fortran 95 code but I'd imagine most people would suggest using something much more recent. – francescalus Jan 25 '21 at 13:08
  • @francescalus Plato 4.83. I think the one important principle in programming is backward compatibility. – VOLCANIC_9 Jan 25 '21 at 13:20
  • 1
    Plato itself isn't a compiler (but an IDE). I'm afraid I can't tell you how to determine which compiler you are using from the IDE, but you are possibly using gfortran (GCC) or the Silverfrost compiler. If you can find the version of whichever you are using and can add that to the question with the requirement to use Fortran 90/95 instead of Fortran 2003/2008/2018 and why you can't use modules, then we can probably reopen the question. – francescalus Jan 25 '21 at 13:39

0 Answers0