Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

VOLATILE

Statement and Attribute: Specifies that the value of an object is entirely unpredictable, based on information local to the current program unit. It prevents objects from being optimized during compilation.

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

Type Declaration Statement:

type, [att-ls, ] VOLATILE [, att-ls] :: object[, object] ...

Statement:

VOLATILE [::] object[, object] ...

type

Is a data type specifier.

att-ls

Is an optional list of attribute specifiers.

object

Is the name of an object, or the name of a common block enclosed in slashes.

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. For example:

  • 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 becomes volatile. If a common block is declared VOLATILE, each variable in the common block becomes volatile.

If an object of derived type is declared VOLATILE, its components become volatile.

If a pointer is declared VOLATILE, the pointer itself becomes volatile.

A VOLATILE statement cannot specify the following:

  • A procedure

  • A namelist group

The VOLATILE attribute must not be specified for a coarray that is accessed by use or host association. A noncoarray object that has the VOLATILE attribute can be associated with an object that does not have the VOLATILE attribute, including by use or host association. A data object with the VOLATILE attribute may not be declared in a PURE procedure or referenced by a PURE statement function.

Example

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

  INTEGER, VOLATILE :: D, E 

The following example shows a VOLATILE statement:

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