FORTRAN, Statements, AUTOMATIC and STATIC
*Conan The Librarian
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. AUTOMATIC and STATIC declarations only affect how data is allocated in storage, as follows: o A variable declared as AUTOMATIC and allocated in memory resides the stack storage area. o 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: o As a keyword in a FUNCTION or SUBROUTINE statement o As a compiler option o 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. The AUTOMATIC attribute is compatible with the ALLOCATABLE, DIMENSION, POINTER, TARGET, and VOLATILE attributes. The STATIC attribute is compatible with the ALLOCATABLE, DIMENSION, POINTER, PRIVATE, PUBLIC, SAVE, TARGET, and VOLATILE attributes.