Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran assignment on declaration and SAVE attribute gotcha

Tags:

In fortran 95, if you assign a variable at declaration

integer :: var = 0

it is equivalent to

integer, save :: var = 0

and the variable is therefore preserved after routine execution (is equivalent to static in C speak) and does not get reinitialized when called again. What is the rationale/technical issue behind such (IMHO dangerous) behavior ?

like image 653
Stefano Borini Avatar asked Jul 28 '10 12:07

Stefano Borini


People also ask

What is declaration in Fortran?

Declaring the type of a Fortran variable is done with type statements. It has the following form: type-specifier :: list. where the type-specifier is one of the following and list is a list of variable names separated with commas: INTEGER : the variables in list can hold integers.

What is Fortran save?

The SAVE statement preserves items in a subprogram after the RETURN or END statements are executed, preventing them from becoming undefined.


2 Answers

I don't think that there is some rationale behind such behavior.

But as far as I know, Stefano, you used wrong terminology. In your code there is no assignment statement only variable (var) initialization using initialization expression (0).

integer :: var = 0 ! type declaration & initialization

integer :: var ! type declaration
var = 0        ! assignment

So it seems that it was just committee design decision. If we have such expression (with equality sign in type declaration statement) it is initialization not assignment. And initialization takes place only once during program (and not procedures) execution.

However there might be some historical reasons for such decision. Take a look at this thread.

Today such behavior is dangerous because many other widely used languages follows another conventions about initialization/assignment.

like image 174
Wildcat Avatar answered Nov 09 '22 13:11

Wildcat


Many old FORTRAN 77 and earlier compilers statically allocated all variables. Many programmers relied upon this behavior -- this was technically a bug in their programs since unless they used the "SAVE" qualifier in the declaration (or added a plain SAVE statement to every procedure) the value of the variable was undefined upon reentry to a procedure. But since in those days programs tended to be tied to a particular platform and compiler for years, programmers got away with this. This is a very common "gotcha" in porting legacy FORTRAN 77 code to a modern Fortran >= 90 compilers. Most compilers provide compile-time switches to restore this behavior, such as the fno-automatic option of gfortran. Most likely the committee viewed variables that were initialized in their declaration as very likely to need the SAVE attribute -- in my opinion, a reasonable design decision. I think what is most different from other languages, and easiest to confuse the multi-lingual programmer, is that the initialization is done only once.

like image 21
M. S. B. Avatar answered Nov 09 '22 14:11

M. S. B.