pTAL Reference Manual (G06.24+, H06.09+, J06.03+)

Example 131 Structure Variants
STRUCT s FIELDALIGN(AUTO);
BEGIN
STRUCT v1;
BEGIN
INT .EXT e; ! e is 4 bytes
END;
STRUCT v2 FIELDALIGN(SHARED8) = v1;
BEGIN
INT .p; ! p is 4 bytes
INT q; ! Compiler reports a warning
END;
END;
In Example 131 (page 192), v1 is 4 bytes, but v2 is 8 bytes. The compiler reports a warning. Data
that your program stores into s.v2.q overwrites the data in the memory locations that follow s.
v2 is 8 bytes to maintain the alignment of variables in memory. For more information about lengths
of pTAL structures, see Chapter 9 (page 114).
Memory Usage for Structured Equivalenced Variables
The memory referenced in an equivalenced declaration must fit within the memory allocated for
the previous variable. When you determine the length of a structure, you must account for filler
that pTAL adds to the structure. In Example 132 (page 192), the equivalenced declaration is not
valid because b is 4 bytes, but a is only 3 bytes. pTAL adds an extra byte at the end of b so that
its total length is an integral multiple of its longest component, i.
Example 132 Memory Usage for Structured Equivalenced Variables (Incorrect)
STRUCT a FIELDALIGN(SHARED2); ! Structure a is 3 bytes
BEGIN
STRING i;
STRING j;
STRING k;
END;
STRUCT b FIELDALIGN(AUTO) = a; ! Structure b is 4 bytes
BEGIN
INT i;
STRING j; ! pTAL adds a byte after field j
END;
If you declare b and then declare a, pTAL does not report an error because a fits within the four
bytes already allocated for b, as in Example 133 (page 193).
192 Equivalenced Variables