5.8. Automatic Data Objects

Automatic data objects are especially useful as working storage in a procedure. These objects can be declared only in procedures or procedure interfaces; they are created when the procedure is entered and disappear when the procedure completes execution. They can be created the same size as an argument to the procedure, so they can be tailored to each invocation.

The following are the three kinds of automatic data objects:

An automatic array or character data object has a specification that depends on the value of a nonconstant expression, is not a dummy argument, and does not have the POINTER or ALLOCATABLE attribute. Automatic arrays are those whose size depends on a variable used in a bound expression. The size of an automatic array or character data object is not known at compile time. The size is calculated at execution time, and storage is allocated upon entry into the procedure. Storage is freed upon exit from the procedure. Local variables and arrays may be declared with the AUTOMATIC attribute. For more information on the AUTOMATIC attribute, see Section 5.9.

The following are examples of automatic data objects:

SUBROUTINE SUB (N, DUMMY_ARRRAY)
COMMON /CB/ K
INTEGER AUTO_ARRAY(N)      ! Automatic array.
CHARACTER(LEN=K*2) CH      ! Automatic character variable.
INTEGER DUMMY_ARRAY(K,N)   ! Not an automatic array
                           ! because it is a dummy
                           ! argument, not a local array.. . .
END SUBROUTINE

An automatic array or character data object is one with a specification that depends on the value of a nonconstant expression and is not a dummy argument. Automatic arrays are those whose size depends on a value used in a bound expression.

An automatic array or character data object is similar to an object declared with the AUTOMATIC attribute. For both items, storage is allocated when the procedure is entered and deallocated when the procedure is exited. The differences between these types are as follows:

In Fortran, the term automatic array or character object does not include noncharacter scalar local variables or arrays with constant bounds. For an array, the extents in each dimension are determined when the procedure is entered. For a character object, the length is determined when the procedure is entered. Apart from dummy arguments, this is the only character object whose length can vary. For arrays, extents can vary for allocatable arrays and array pointers as well as dummy arguments. An automatic array or character object is not a dummy argument, but it is declared with a specification expression that is not a constant expression. The specification expression can be the length of the character object or the bounds of the array. For variables declared with the AUTOMATIC attribute, the variables must be scalar or array values with constant bounds, and they cannot be declared in a common block or module. These variables are allocated on the stack. Automatic objects cannot be saved or initialized.

In the following example, C is an automatic array and MESSAGE is an automatic character object:

SUBROUTINE SWAP_ARRAYS(A, B, A_NAME, B_NAME)
   REAL, DIMENSION(:), INTENT(INOUT) :: A, B
   CHARACTER(LEN = *), INTENT(IN)    :: A_NAME, B_NAME

   REAL C(SIZE (A))
   CHARACTER (LEN = LEN(A_NAME) + LEN(B_NAME) + 17) MESSAGE

   C = A
   A = B
   B = C

   MESSAGE = A_NAME // " and " // B_NAME // " are swapped"
   PRINT *,  MESSAGE
END SUBROUTINE SWAP_ARRAYS

Note: The Fortran standard does not provide a means to explicitly declare automatic variables as automatic.