My question is about array allocation in Fortran.
I have a subroutine, say readParams, where I want to read some dynamically sized arrays from files. These are also used outside the subroutine. What is the best way to handle this?
In F95 it seems to be impossible to allocate within the subroutine and pass the arrays, filled with values, back to the main program. But if I allocate it in the main program and use "intent(inout)" in the subroutine it also gets deallocated there.
(I'm using F90/95 here, but since the code is not large I could also modify it to a newer version... I'm rather new to Fortran, so I'm unsure if an improvement of array handling is worthwhile the time investment^^
EDIT Thanks for the hint. I am not trying to deallocate my arrays within a subroutine though.
The problem is: I have an array which I need to allocate somewhere within my main program. The arraysize is known only after I read it from an input in subroutine readArgs. Therefore I make the array "allocatable". Once allocated that status must never change again. The array is filled with values in a subroutine readParams. Do I allocate it best in main or in readParams and how?
... I have now put my subroutines in a module and use them from there. At the moment I do the allocation in main, pass the arrays to my subroutine and have removed the "allocatable" statement in the array declaration in the subroutine. It seems to work but I still don't really understand if this is the way to go.
readParams
allocatable. 3. Allocate the array inreadParams
. Really look at stackoverflow.com/a/13810698/721644. You can also just allocate the array in the main program. If you have problems with that, show your code and error messages and explain those problems. – Vladimir FreaArgs
orreadParams
, it is your choice. If you believe the link is not applicable to your situation, then 1. show your code, 2. show any error messages you got when you tried to allocate the array. – Vladimir F