Variable size arrays in Fortran without Allocate() Variable size arrays in Fortran without Allocate() arrays arrays

Variable size arrays in Fortran without Allocate()


Fortran can automatically create arrays just with declarations on entry to subroutines, as long as the the dimensions are known at run time ... this doesn't require the dimensions to be declared parameter attribute, they can be arguments, e.g.,

subroutine MySub ( N )integer, intent (in) :: Nreal, dimension (N) :: array

is valid. So why not decide your size "N" in the main program, or some subroutine, then call another subroutine to continue. Likely with this approach the array will be on the stack. As @eriktous wrote, the Fortran language standard doesn't specify this behavior. Some compilers switch local arrays to the heap past a certain size. Some compilers provide options to control this behavior. Placing large arrays on the heap would probably be overridden with recursive or OpenMP.

You can also pass an allocatable array as an actual argument to a subroutine without the dummy argument of the subroutine being declared as allocatable. Which may not help with your concern because the original array will still likely be on the heap.


The Fortran standard has no concept of stack and heap, so this will be implementation (i.e. compiler) dependent. Looking at the docs for e.g. gfortran, it has an option

-frecursive    Allow indirect recursion by forcing all local arrays to be allocated on the stack.

Other compilers may have similar options. Perhaps this does what you want.