Visible to Intel only — GUID: GUID-0C69B3BC-A153-4D34-A973-A3E02CBDAA82
Visible to Intel only — GUID: GUID-0C69B3BC-A153-4D34-A973-A3E02CBDAA82
AUTOMATIC
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:
Type Declaration Statement:
type, [att-ls,] AUTOMATIC [, att-ls] :: v[, v] ...
Statement:
AUTOMATIC [::] 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. |
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:
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 scalar variables on the stack. Other non-allocatable variables of non-recursive subprograms are allocated in static storage by default. This default can be changed through compiler options. Appropriate use of the SAVE attribute may be required if your program assumes that local variables retain their definition across subprogram calls.
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.
Use the heap-arrays compiler option to avoid stack overflows at runtime by placing large automatic arrays in the heap instead of on the stack.
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 saved, except for "var1" and "var3". These are
C explicitly declared in an AUTOMATIC statement, and thus have
C memory locations on the stack:
SUBROUTINE DoIt (arg1, arg2)
INTEGER(4) arg1, arg2
INTEGER(4) var1, var2, var3, var4
SAVE
AUTOMATIC var1, var3
C var2 and var4 are saved