XL Fortran for AIX 8.1

Language Reference

SAVE

Purpose

The SAVE attribute specifies the names of objects and named common blocks whose definition status you want to retain after control returns from the subprogram where you define the variables and named common blocks.

Format



>>-SAVE--+-----------------------------------------+-----------><
         |         .-,---------------------------. |
         |         V                             | |
         '-+----+----+-object_name-------------+-+-'
           '-::-'    '-/--common_block_name--/-'
 
 

Rules

A SAVE statement without a list is treated as though it contains the names of all common items and local variables in the scoping unit. A common block name having the SAVE attribute has the effect of specifying all the entities in that named common block.

Within a function or subroutine subprogram, a variable whose name you specify with the SAVE attribute does not become undefined as a result of a RETURN or END statement in the subprogram.

object_name cannot be the name of a dummy argument, pointee, procedure, automatic object, or common block entity.

If a local entity specified with the SAVE attribute (and not in a common block) is in a defined state at the time that a RETURN or END statement is encountered in a subprogram, that entity is defined with the same value at the next reference of that subprogram. Saved objects are shared by all instances of the subprogram.

+-------------------------------IBM Extension--------------------------------+

XL Fortran permits function results to have the SAVE attribute. To indicate that a function result is to have the SAVE attribute, the function result name must be explicitly specified with the SAVE attribute. That is, a SAVE statement without a list does not provide the SAVE attribute for the function result.

Variables declared as SAVE are shared amongst threads. To thread-safe an application that contains shared variables, you must either serialize access to the static data using locks, or make the data thread-specific. One method of making the data thread-specific is to move the static data into a named COMMON block that has been declared THREADLOCAL. The Pthreads library module provides mutexes to allow you to serialize access to the data using locks. See Chapter 17, Pthreads Library Module for more information. The lock_name attribute on the CRITICAL directive also provides the ability to serialize access to data. See CRITICAL / END CRITICAL for more information. The THREADLOCAL directive ensures that common blocks are local to each thread. See THREADLOCAL for more information.

+----------------------------End of IBM Extension----------------------------+

Attributes Compatible with the SAVE Attribute


  • ALLOCATABLE
  • DIMENSION
  • POINTER

  • PRIVATE
  • PUBLIC
  • STATIC

  • TARGET
  • VOLATILE

Examples

LOGICAL :: CALLED=.FALSE.
CALL SUB(CALLED)
CALLED=.TRUE.
CALL SUB(CALLED)
CONTAINS
  SUBROUTINE SUB(CALLED)
    INTEGER, SAVE :: J
    LOGICAL :: CALLED
    IF (CALLED.EQV..FALSE.) THEN
      J=2
    ELSE
      J=J+1
    ENDIF
    PRINT *, J                  ! Output on first call is 2
                                ! Output on second call is 3
  END SUBROUTINE
END

Related Information


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]