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

Example 66 Structure With SHARED2 Field Alignment
STRUCT s1 FIELDALIGN(SHARED2);
BEGIN
INT i; ! i begins at offset: 0
INT(32) j; ! j begins at offset: 2
STRING s1; ! s1 begins at offset: 6
STRING s2; ! s1 begins at offset: 7
FIXED f; ! f begins at offset: 8
INT k; ! k begins at offset: 16
END; ! Total length of s1: 18 bytes
Structures that specify SHARED8 field alignment, however, require you to explicitly declare filler
items to force fields to be well-aligned, as previously described. You might be able to reduce the
size of a structure if you can arrange its fields to minimize the number of filler items required.
In Example 67 (page 132), the structure s2 has the same fields as s1 in Example 66 (page 132),
but s2 has SHARED8 field alignment and includes filler items where required. Offsets are shown
in bytes. s2 is 32 bytes.
Example 67 Structure With SHARED8 Field Alignment
STRUCT s2 FIELDALIGN(SHARED8);
BEGIN
INT i; ! i begins at offset 0
FILLER 2; ! 2 bytes of filler
INT(32) j; ! j begins at offset 4
STRING s1; ! s1 begins at offset 8
STRING s2; ! s2 begins at offset 9
FILLER 6; ! 6 bytes of filler
FIXED f; ! f begins at offset 16
INT k; ! k begins at offset 24
FILLER 6; ! Pad to a multiple of the widest field (f)
END; ! Total length of s2: 2 bytes
s2 has SHARED8 field alignment, and uses 14 more bytes than s1. If the order of the fields within
the structure is not important; however, you can rearrange the fields so that the structure contains
fewer bytes, as shown in Example 68 (page 132).
Example 68 Optimized Structure With SHARED8 Field Alignment
STRUCT s3 FIELDALIGN(SHARED8);
BEGIN
INT i; ! i begins at offset 0
STRING s1; ! s1 begins at offset 2
STRING s2; ! s2 begins at offset 3
INT(32) j; ! j begins at offset 4
FIXED f; ! f begins at offset 8
INT k; ! k begins at offset 16
FILLER 6; ! Pad to a multiple of the widest field (f)
END; ! Total length of s3: 24 bytes
By rearranging the order of the fields, s3 requires 24 bytes, rather than the 32 bytes required by
s2, even though the information in s3 and s2 is the same. s3 uses only six more bytes than s1.
Structure Length
The total number of bytes in a SHARED8 structure must be an integral multiple of the widest field
in the structure. If needed, you must explicitly declare filler at the end of a SHARED8 structure to
ensure this condition.
132 Structures