Developer Guide and Reference

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

UNION and END UNION

Statements: Define a data area that can be shared intermittently during program execution by one or more fields or groups of fields. A union declaration must be within a structure declaration.

Each unique field or group of fields is defined by a separate map declaration.

UNION

   map-declaration

   map-declaration

   [map-declaration]

   . . .

   [map-declaration]

END UNION

map-declaration

Takes the following form:

MAP

   field-declaration

   [field-declaration]

   . . .

   [field-declaration]

END MAP

field-declaration

Is a structure declaration or RECORD statement contained within a union declaration, a union declaration contained within a union declaration, or the declaration of a data field (having a data type) within a union. It can be of any intrinsic or derived type.

As with normal Fortran type declarations, data can be initialized in field declaration statements in union declarations. However, if fields within multiple map declarations in a single union are initialized, the data declarations are initialized in the order in which the statements appear. As a result, only the final initialization takes effect and all of the preceding initializations are overwritten.

The size of the shared area established for a union declaration is the size of the largest map defined for that union. The size of a map is the sum of the sizes of the fields declared within it.

Manipulating data by using union declarations is similar to using EQUIVALENCE statements. The difference is that data entities specified within EQUIVALENCE statements are concurrently associated with a common storage location and the data residing there; with union declarations you can use one discrete storage location to alternately contain a variety of fields (arrays or variables).

With union declarations, only one map declaration within a union declaration can be associated at any point in time with the storage location that they share. Whenever a field within another map declaration in the same union declaration is referenced in your program, the fields in the prior map declaration become undefined and are succeeded by the fields in the map declaration containing the newly referenced field.

Example

In the following example, the structure WORDS_LONG is defined. This structure contains a union declaration defining two map fields. The first map field consists of three INTEGER*2 variables (WORD_0, WORD_1, and WORD_2), and the second, an INTEGER*4 variable, LONG:

 STRUCTURE /WORDS_LONG/
   UNION
     MAP
       INTEGER*2  WORD_0, WORD_1, WORD_2
     END MAP
     MAP
       INTEGER*4  LONG
     END MAP
   END UNION
 END STRUCTURE

The length of any record with the structure WORDS_LONG is 6 bytes. The following figure shows the memory mapping of any record with the structure WORDS_LONG:

Memory Map of Structure WORDS_LONG

In the following example, note how the first 40 characters in the string2 array are overlayed on 4-byte integers, while the remaining 20 are overlayed on 2-byte integers:

  UNION
     MAP
       CHARACTER*20 string1, CHARACTER*10 string2(6)
     END MAP
     MAP
       INTEGER*2 number(10), INTEGER*4 var(10), INTEGER*2
 +      datum(10)
     END MAP
  END UNION