pTAL Reference Manual (G06.24+, H06.09+, J06.03+)
Structure Alignment Examples
The following examples illustrate how your structure data layout is affected by structure alignment.
Only SHARED8 structures are shown because SHARED2 structures are not well-aligned. pTAL
always generates conservative code for references to fields of a SHARED2 structure that are more
than 16 bits long.
Figure 4 (page 120) shows a structure, s1, that specifies FIELDALIGN(SHARED8). Because the
widest field in the structure, f, is a FIXED field, the base alignment of s1 is 8. To be well-aligned,
s1 must be allocated at a memory address that is an integral multiple of eight. Filler is added as
follows:
• Before i32 so that i32 begins at an offset that is a multiple of four relative to the beginning
of the structure.
• Before f so that f begins at an offset that is a multiple of eight relative to the beginning of
the structure.
• At the end of the structure so that the total length of the structure is an integral multiple of the
widest field in the structure.
Figure 4 Alignment of SHARED8 Structure With Base Alignment of 8
STRUCT s FIELDALIGN(SHARED8); ! Base alignment of s1 is 8
BEGIN
INT i; ! Begins at offset 0
FILLER 2; ! 2 bytes of filler required
INT(32) i32; ! Begins at offset 4
STRING s1; ! Begins at offset 8
STRING s2; ! Begins at offset 9
FILLER 6; ! 6 bytes of filler required
FIXED f; ! Begins at offset 16
INT k; ! Begins at offset 24
FILLER 6; ! Must pad to multiple of widest field, f
END; ! Total length of s1: 32 bytes
Figure 5 (page 121) shows which fields of s1 are misaligned if the base of the structure in memory
is not at an integral multiple of its base alignment. Only structures whose base is at an even-byte
address are shown. Accessing structures whose base is at an odd-byte offset produces undefined
results. For more information, see Overview of Structure Alignment (page 116).
120 Structures