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:
Statement:
AUTOMATIC and STATIC declarations only affect how data is allocated in storage, as follows:
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:
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.
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: