[go: nahoru, domu]

Jump to content

Union type

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Samiam95124 (talk | contribs) at 01:30, 13 July 2023 (Undid revision 1165106628 by Samiam95124 (talk)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Pascal

In Pascal, there are two ways to create unions. One is the standard way through a variant record. The second is a nonstandard means of declaring a variable as absolute, meaning it is placed at the same memory location as another variable or at an absolute address. While all Pascal compilers support variant records, only some support absolute variables.

For the purposes of this example, the following are all integer types: a byte consists of 8 bits, a word is 16 bits, and an integer is 32 bits.

The following example shows the non-standard absolute form:

var
    A: Integer;
    B: array[1..4] of Byte absolute A;
    C: Integer absolute 0;

In the first example, each of the elements of the array B maps to one of the specific bytes of the variable A. In the second example, the variable C is assigned to the exact machine address 0.

In the following example, a record has variants, some of which share the same location as others:

type
     Shape = (Circle, Square, Triangle);
     Dimensions = record
        case Figure: Shape of 
           Circle: (Diameter: real);
           Square: (Width: real);
           Triangle: (Side: real; Angle1, Angle2: 0..360)
        end;