Variable size arrays in Fortran without Allocate()

Viewed 19196

Is there a way to create variable size arrays in Fortran on the stack? Allocate() does not work for me, because it places the array on the heap. This may lead to problems with parallelization (see my other question: OpenMP: poor performance of heap arrays (stack arrays work fine) ). Of course, some smart memory management would give a way around that problem, but memory management in Fortran sounds silly.

Essentially, I am looking for a Fortran equivalent of the following in C:

scanf("%d", N);
int myarray[N];

To re-iterate: I do NOT want

Integer, PARAMETER :: N=100
Integer, Dimension(N) :: myarray

because this determines the array size at compile time. Neither do I want

Integer, Dimension(:), Allocatable :: myarray
read(*,*) N
Allocate(myarray(1:N))

because it places the array on the heap.

Help very much appreciated. I was very happy with Allocatable arrays until my recent encounter with the problem in the question cited above. If there is a negative answer to this question, I would very much appreciate a link to the source.

Edit: see comments to M.S.B.'s answer. An elegant way of doing this only became possible in Fortran 2008, and it is done in a block construct.

2 Answers
Related