Are local variables in Fortran 77 static or stack dynamic?

In Fortran 77 & 90/95/2003, if you want the value of a variable local to a subroutine preserved across subroutine calls, you should declare it the “save” attribute, e.g., (using Fortran 90 style):

integer, save :: counter

OR

integer :: counter
save :: counter

.
Or, if you want the “save” behavior to apply to all variables just include in the subroutine a simple

save

statement without any variables.
In Fortran 90, a variable initialization in a declaration,

integer :: counter = 0

automatically acquires the save attribute. I don’t think that this was the case in Fortran 77.

This is one area in which experiments could be misleading — they will tell you what a particular compiler does, but perhaps not what the Fortran 77 language standard is, nor what other compilers did. Many old Fortran 77 compilers didn’t place local variables on the stack and implicitly all variables had the save attribute, without the programming having used that declaration. This, for example, was the case with the popular DEC Fortran compilers. It is common for legacy Fortran 77 programs that were used only with a particular compiler of this type to malfunction with a modern compiler because programmers forgot to use the save attribute on variables that needed it. Originally this didn’t cause a problem because all variables effectively had the save attribute. Most modern compilers place local variables without save on the stack, and these programs frequently malfunction because some variables that need “save” “forget” their values across subroutine calls. This can be fixed by identifying the problem variables and adding save (work), adding a save statement to every subroutine (less work), or many compilers have an option (e.g., -fno-automatic in gfortran) to restore the old behavior (easy).

It seems a peculiar question — you won’t find out about “Fortran 77” but about a particular compiler. And why use Fortran 77 instead of Fortran 95/2003? Does the prof. think Fortran stopped in 1977?

Leave a Comment