21

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 ?

3
  • 2
    For the sake of completeness, let me mention that in the Fortran 2003 standard the point you mention is covered in C1107: "If an object of a type for which component-initialization is specified (R444) appears in the specification-part of a module and does not have the ALLOCATABLE or POINTER attribute, the object shall have the SAVE attribute.". Commented Jul 28, 2010 at 13:01
  • @Alexandros Gezerlis - Right, but he's asking "why" did they do things this way, and now that I understand his question, I also have the same question :). Commented Jul 28, 2010 at 13:03
  • I know, that's why I didn't post this as an answer. Commented Jul 28, 2010 at 13:08

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

5 Comments

If this is a deliberate design decision, should I maybe post a question "what are Fortran committee members allowed to smoke during decisional meetings?" :) seriously. I don't think they screwed up so badly, so there must be a reason, maybe rooted in compatibility or technical issues. it seems too strange to be a design decision for its own sake.
@Stefano: also there might be some historical reasons and not smoking. =) Read this thread: rhinocerus.net/forum/lang-fortran/…
I think you just hit the spot! Please edit your answer to include the link.
The link is broken now. What was the gist of the article?
@Wildcat, there is always a rationale from J3 for a change to the Fortran standard. You can find documents dating back to at least 1987 at j3-fortran.org/doc/year. If you look (as I won't) I suspect that you'll find that real :: x = 42. is intended to replace the two statements of real x followed by data /x/42.. (data syntax might be messed up here as I rarely use data statements.) So, the semantics of an initialization expression are inherited from data statements.
10

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.

1 Comment

The Fortran 77 standard didn't specify whether reinitialization would occur. I used compilers of both kinds; the practice of initialization without SAVE wasn't portable. I recall seeing a statement that the committee reasoned that a separate assignment clearly takes care of the reinitialization case, and allows applying or removing a SAVE qualifier without affecting the result, while the initialize once behavior wouldn't be clean to implement with the other choice in the standard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.