Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 11/07/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

SAVE

Statement and Attribute: Causes the values and definition of objects to be retained after execution of a RETURN or END statement in a subprogram.

The SAVE attribute can be specified in a type declaration statement or a SAVE statement, and takes one of the following forms:

Type Declaration Statement:

type,[att-ls,] SAVE [, att-ls] :: entity[, entity ] ...

Statement:

SAVE [[::]entity [, entity ] ...]

type

Is a data type specifier.

att-ls

Is an optional list of attribute specifiers.

entity

Is the name of an object, the name of a procedure pointer, or the name of a common block enclosed in slashes (/common-block-name/).

Description

In Intel® Fortran, certain variables are given the SAVE attribute, or not, by default. Variables are implicitly given the SAVE attribute depending on whether a program unit is compiled for recursion.

NOTE:

In the following lists, "initialized" means that the variable has been given an initial value in a DATA statement, it has been initialized in a type declaration statement, it is of a derived type that is default initialized, or it has a component that is default initialized.

The following variables are not saved by default:

  • Scalar variables that are local to a recursive procedure and are not initialized (see above Note)

  • Arrays of any type that are local to a recursive procedure and are not initialized (see above Note)

  • Variables that are declared AUTOMATIC

  • Local variables with the ALLOCATABLE attribute

The following variables are saved by default:

  • Variables in COMMON blocks

  • Scalar variables not of intrinsic types INTEGER, REAL, COMPLEX, and LOGICAL that are local to a non-recursive subprogram

  • Non-scalar local variables of non-recursive subprograms

  • Module or submodule variables

  • Initialized (see above Note) variables

  • RECORD variables that are data initialized by default initialization specified in its STRUCTURE declaration

Local variables that are not described in the preceding two lists are saved by default.

NOTE:

Certain compiler options (such as options [Q]save, assume norecursion, and auto) and the use of OpenMP* features can change the defaults.

NOTE:

Coarrays that do not have the ALLOCATABLE attribute and are local to the main program unit or to a procedure are not statically allocated, but are allocated at program startup. They are not deallocated until the program terminates.

To enhance portability and avoid possible compiler warning messages, Intel recommends that you use the SAVE statement to name variables whose values you want to preserve between subprogram invocations.

When a SAVE statement does not explicitly contain a list, all allowable items in the scoping unit are saved.

A SAVE statement cannot specify the following (their values cannot be saved):

  • A blank common

  • An object in a common block

  • A procedure

  • A dummy argument

  • A function result

  • An automatic object

  • A PARAMETER (named) constant

Even though a common block can be included in a SAVE statement, individual variables within the common block can become undefined (or redefined) in another scoping unit.

If a common block is saved in any scoping unit of a program (other than the main program), it must be saved in every scoping unit in which the common block appears.

A SAVE statement has no effect in a main program.

Example

The following example shows a type declaration statement specifying the SAVE attribute:

SUBROUTINE TEST()
  REAL, SAVE :: X, Y

The following is an example of the SAVE statement:

SAVE A, /BLOCK_B/, C, /BLOCK_D/, E

The following shows another example:

  SUBROUTINE MySub
    COMMON /z/ da, in, a, idum(10)
    real(8) x,y
    ...

    SAVE x, y, /z/
! alternate declaration
    REAL(8), SAVE :: x, y
    SAVE /z/