STATIC

Statement and Attribute: Controls the storage allocation of variables in subprograms (as does AUTOMATIC). Variables declared as STATIC and allocated in memory reside in the static storage area, rather than in the stack storage area.

The STATIC attribute can be specified in a type declaration statement or a STATIC statement, and takes one of the following forms:

Syntax

Type Declaration Statement:

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

Statement:

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

STATIC 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.

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 following ways:

To override any compiler option that may affect variables, explicitly specify the variables as 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 STATIC more than once in the same scoping unit.

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

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

Variable STATIC
Dummy argument No
Automatic object No
Common block item Yes
Use-associated item No
Function result No
Component of a derived type 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.

Compatibility

CONSOLE STANDARD GRAPHICS QUICKWIN GRAPHICS WINDOWS DLL LIB

See Also: AUTOMATIC, SAVE, Type declaration statements, Compatible attributes, RECURSIVE, /recursive, OPTIONS, POINTER, Modules and Module Procedures

Examples

The following example shows a type declaration statement specifying the STATIC attribute:

INTEGER, STATIC :: ARRAY_A

The following example uses a 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
...
 INTEGER N1, N2
 N1 = -1
 DO WHILE (N1)
   N2 = N1*2
   call sub1(N1, N2)
   read *, N1
 END DO
 CONTAINS
 SUBROUTINE sub1 (iold, inew)
 INTEGER, intent(INOUT):: iold
 integer, STATIC ::N3
 integer, intent(IN) :: inew
 if (iold .eq. -1) then
   N3 = iold
 end if
 print *, 'New: ', inew, 'N3: ',N3
 END subroutine
 !
 END