[go: nahoru, domu]

Jump to content

Union type: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Tag: Reverted
Undid revision 1165105851 by Bubba73 (talk)
Tags: Undo Reverted
Line 2: Line 2:
In [[Pascal (programming language)|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.
In [[Pascal (programming language)|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.
For the purposes of this example, the following are all integer types: a '''byte''' is 8-bits, a '''word''' is 16-bits, and an '''integer''' is 32-bits.


The following example shows the non-standard absolute form:
The following example shows the non-standard absolute form:

Revision as of 01:29, 13 July 2023

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 is 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;