| Fortran Language Reference Manual, Volume 1 - S-3692-51 | ||
|---|---|---|
| Prev Section | Chapter 5. Declarations | Next Section |
The VOLATILE attribute and statement specifies that the value of an object is unpredictable. The object's value can change without visible assignment by the program, and it's value can be affected by external events. The presence of this statement prevents the compiler from optimizing references to specified variables, arrays, and common blocks of data.
Note: The VOLATILE attribute and statement are Fortran 2003 features.
The following format is for a type declaration statement with the VOLATILE attribute:
type, VOLATILE [, attribute_list] :: entity_decl_list |
Subject to the rules governing combinations of these attributes, attribute_list can contain the following:
| ALLOCATABLE |
| AUTOMATIC (EXTENSION) |
| DIMENSION |
| INTENT |
| OPTIONAL |
| POINTER |
| PRIVATE |
| PROTECTED (F2003) |
| PUBLIC |
| SAVE |
| STATIC (EXTENSION) |
| TARGET |
| VALUE (F2003) |
The entity_decl_list can include the name of a common block, enclosed in slash characters (for example, /common_block_name/).
The format for the VOLATILE statement is as follows:
Table 5-25.
EXT | volatile_stmt | is |
| |
EXT | entity_decl_list | is |
| |
EXT |
| or |
|
The following example shows a type declaration statement that specifies the VOLATILE attribute:
INTEGER, VOLATILE :: D, E |
In the following example, the named common block, BLK1, and the variables D and E are volatile. Variables P1 and P4 become volatile because of the direct equivalence of P1 and the indirect equivalence of P4. The code that shows this is as follows:
PROGRAM TEST LOGICAL(KIND=1) IPI(4) INTEGER(KIND=4) A, B, C, D, E, ILOOK INTEGER(KIND=4) P1, P2, P3, P4 COMMON /BLK1/A, B, C VOLATILE /BLK1/, D, E EQUIVALENCE(ILOOK, IPI) EQUIVALENCE(A, P1) EQUIVALENCE(P1, P4) |
The presence of a VOLATILE attribute or statement can inhibit some optimizations because it asserts that the compiler must perform loads and stores from the specified objects. As an example, consider the following code fragment:
J = 1 DO I = 1,100000 IF (J.EQ.2) PRINT 'FOO' END DO |
If the preceding code were included in a Fortran program, the compiler might remove the statement IF (J.EQ.2) PRINT 'FOO' because J is loop invariant and because J was previously assigned the value 1. If J were declared VOLATILE, the compiler would perform all loads of J because something else might affect the value of J.
Note: The Fortran standard does not describe the VOLATILE attribute or statement.
A variable or common block must be declared VOLATILE if it can be read or written in a way that is not visible to the compiler. This would be the case in the following situations:
If an operating system feature is used to place a variable in shared memory so that it can be accessed by other programs, the variable must be declared VOLATILE.
If a variable is accessed or modified by a routine called by the operating system when an asynchronous event occurs, the variable must be declared VOLATILE.
If an array is declared VOLATILE, each element in the array is VOLATILE. If a common block is declared VOLATILE, each variable in the common block is VOLATILE.
If an object of derived type is declared VOLATILE, its components are VOLATILE.
If a pointer is declared VOLATILE, the pointer itself is VOLATILE.
A VOLATILE statement must not specify a procedure, function result, or NAMELIST group name.
| Prev Section | Table of Contents | Title Page | Index | Next Section |
| SAVE and STATIC Attributes and Statements | Up one level | NAMELIST Statement |