I've read about the save
statement in the (Intel's) language reference document, but I cannot quite grasp what it does. Could someone explain to me in simple language what it means when the save
statement is included in a module ?
相关问题
- CABS(x) function for complex(8)
- Upper bound of random number generator
- Finding number of lines of a data file using comma
- When do Fortran module allocatable arrays go out o
- How are these double precision values accurate to
相关文章
- Does gfortran take advantage of DO CONCURRENT?
- Fortran 90 - “Segmentation fault - invalid memory
- Reading records from a Fortran binary file in Pyth
- expand a dense matrix
- Unable to use f2py to link large PETSc/SLEPc Fortr
- How to read comma delimited data file if some data
- getting Cygwin to know about microsoft cl and nmak
- How do I allocate input arrays with f2py?
Posting this as an answer to M.S.B. because the lack of formatting in comments would probably make a pig's breakfast out of the whole thing:
First, thanks for answering, both of you. I appreciate it.
If I understood right;
were it not for the SAVE statement in the above little example, the variable i (the value of the variable) would be "lost" after the first call of save subroutine. Thanks to it, it retains it's value - "1" in this case, and because of it, it increases to "2" during the second call.
Have I gotten it right ? Near maybe ?
Normally, local variables go out of scope once execution leaves the current procedure, and so have no 'memory' of their value on previous invocations.
SAVE
is a way of specifying that a variable in a procedure should maintain its value from one call to the next. It's useful when you want to store state in a procedure, for example to keep a running total or maintain a variable's configuration.There's a good explanation here, with an example.
A short explanation could be: the attribute
save
says that the value of a variable must be preserved across different calls to the same subroutine/function. Otherwise normally when you return from a subroutine/function, "local" variables lose their values since the memory where those vars were stored is released. It is likestatic
in C, if you know this language.In principal when a module goes out-of-scope, the variables of that module become undefined -- unless they are declared with the SAVE attribute, or a SAVE statement is used. "Undefined" means that you are not allowed to rely on the variable having the previous value if you again use the module -- it might have the previous value when you re-access the module, or it might not -- there is no guarantee. But many compilers don't do this for module variables -- the variables probably retain their values -- it isn't worth the effort for the compiler to figure out whether a module remains in scope or not and probably module variables are treated as global variables -- but don't rely on that! To be safe, either use "save" or "use" the module from the main program so that it never goes out of scope.
"save" is also important in procedures, to store "state" across invocations of the subroutine or function (as written by @ire_and_curses) -- "first invocation" initializations, counters, etc.
etc.
In this code fragment, "counter" will report the number of invocations of subroutine x. Though actually in Fortran >=90 one can omit the "save" because the initialization in the declaration implies "save".
In contrast to the module case, with modern compilers, without the save attribute or initialization-on-a-declaration, it is normal for local variables of procedures to lose their values across invocations. So if you attempt to use "var" on an later call before redefining it in that call, the value is undefined and probably won't be the value calculated on a previous invocation of the procedure.
This is different from the behavior of many FORTRAN 77 compilers, some of which retained the values of all local variables, even though this wasn't required by the language standard. Some old programs were written relying on this non-standard behavior -- these programs will fail on the newer compilers. Many compilers have an option to use the non-standard behavior and "save" all local variables.
LATER EDIT: update with a code example that shows incorrect usage of a local variable that should have the save attribute but doesn't:
Local variable k of the subroutine is intentionally misused -- in this program it is initialized in the first call since control is TRUE, but on the second call control is FALSE, so k is not redefined. But without the save attribute k is undefined, so the using its value is illegal.
Compiling the program with gfortran, I found that k retained its value anyway:
Compiling the program with ifort and aggressive optimization options, k lost its value:
Using ifort with debugging options, the problems was detected at runtime!