Statement and Attribute: Controls the storage allocation of variables in subprograms (as does STATIC). Variables declared as AUTOMATIC and allocated in memory reside in the stack storage area, rather than at a static memory location.
The AUTOMATIC attribute can be specified in a type declaration statement or an AUTOMATIC statement, and takes one of the following forms:
Syntax
Description
AUTOMATIC declarations only affect how data is allocated in storage.
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.
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 more than once in the same scoping unit.
If the variable is a pointer, AUTOMATIC applies only to the pointer itself, not to any associated target.
Some variables cannot be specified as AUTOMATIC. The following table shows these restrictions:
Variable | AUTOMATIC |
---|---|
Dummy argument | No |
Automatic object | No |
Common block item | No |
Use-associated item | No |
Function result | No |
Component of a derived type | No |
If a variable is in a module's outer scope, it cannot be specified as AUTOMATIC.
See Also
STATIC, SAVE, Type declaration statements, Compatible attributes, RECURSIVE, OPTIONS, POINTER, Modules and Module Procedures, recursive compiler option
Examples
The following example shows a type declaration statement specifying the AUTOMATIC attribute:
REAL, AUTOMATIC :: A, B, C
The following example uses an AUTOMATIC 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
...
C In this example, all variables within the program unit
C are automatic, except for "var1" and "var2"; these are
C explicitly declared in a SAVE statement, and thus have
C static memory locations:
SUBROUTINE DoIt (arg1, arg2)
INTEGER(4) arg1, arg2
INTEGER(4) var1, var2, var3, var4
AUTOMATIC
SAVE var1, var3
C var2 and var4 are automatic