# 5.3 AUTOMATIC and STATIC Attributes and Statements

The AUTOMATIC and STATIC attributes control the storage allocation of variables in subprograms.

The AUTOMATIC and STATIC attributes can be specified in a type declaration statement or an AUTOMATIC or STATIC statement, and take one of the following forms:

Type Declaration Statement:

  type, [att-ls,] AUTOMATIC [, att-ls] :: v [, v]...
  type, [att-ls,] STATIC [, att-ls] :: v [, v]...

Statement:

  AUTOMATIC v [, v]...
  STATIC v [, v]...
  type
  Is a data type specifier.
  att-ls
  Is an optional list of attribute specifiers.
  v
  Is the name of a variable or an array specification. It can be of any type.

Rules and Behavior

AUTOMATIC and STATIC declarations only affect how data is allocated in storage, as follows:

- A variable declared as AUTOMATIC and allocated in memory resides in the stack storage area.
- A variable declared as STATIC and allocated in memory resides in the static storage area.

If you want to retain definitions of variables upon reentry to subprograms, you must use the SAVE attribute.

Automatic variables can reduce memory use because only the variables currently being used are allocated to memory.

Automatic variables allow possible recursion. With recursion, a subprogram can call itself (directly or indirectly), and resulting values are available upon a subsequent call or return to the subprogram. For recursion to occur, RECURSIVE must be specified in one of the following ways:

- As a keyword in a FUNCTION or SUBROUTINE statement
- As a compiler option
- As an option in an OPTIONS statement

By default, the compiler allocates local variables of non-recursive subprograms, except for allocatable arrays, in the static storage area. The compiler may choose to allocate a variable in temporary (stack or register) storage if it notices that the variable is always defined before use. Appropriate use of the SAVE attribute can prevent compiler warnings if a variable is used before it is defined.

To change the default for variables, specify them as AUTOMATIC or specify RECURSIVE (in one of the ways mentioned above).

To override any compiler option that may affect variables, explicitly specify the variables as AUTOMATIC or STATIC.

  Note: Variables that are data- initialized, and variables in COMMON and SAVE statements are always static. This is regardless of whether a compiler option specifies recursion.

A variable cannot be specified as AUTOMATIC or STATIC more than once in the same scoping unit.

If the variable is a pointer, AUTOMATIC or STATIC apply only to the pointer itself, not to any associated target.

Some variables cannot be specified as AUTOMATIC or STATIC. The following table shows these restrictions:

Variable | AUTOMATIC | STATIC
--- | --- | ---
Dummy argument | No | No
Automatic object | No | No
Common block item | No | Yes
Use-associated item | No | No
Function result | No | No
Component of a derived type | No | No

A variable can be specified with both the STATIC and SAVE attributes.

If a variable is in a module's outer scope, it can be specified as STATIC, but not as AUTOMATIC.

Examples

The following examples show type declaration statements specifying the AUTOMATIC and STATIC attributes:

    REAL, AUTOMATIC :: A, B, C
    INTEGER, STATIC :: ARRAY_A

The following example shows an AUTOMATIC AND STATIC statement:

    ...
    CONTAINS
     INTEGER FUNCTION REDO_FUNC
       INTEGER I, J(10), K
       REAL C, D, E(30)
       AUTOMATIC I, J, K(20)
       STATIC C, D, E
       ...
     END FUNCTION
    ...

For More Information:

- On type declaration statements, see Section 5.1.
- On subprograms, see Section 8.5.
- On specifying recursive subprograms, see Section 8.5.1.1.
- On the OPTIONS statement, see Section 14.2.
- On compiler options, see your user manual or programmer's guide.
- On compatible attributes, see Table 5-1.
- On the SAVE attribute, see Section 5.17.
- On pointers, see Section 5.15.
- On modules, see Section 8.3.

Previous Page Next Page Table of Contents