pTAL Conversion Guide

Structures
pTAL Conversion Guide527302-002
11-26
Optimizing Structure Layouts
Structure s2 in Example 11-9 on page 11-26 has the same fields as structure s1 in
Example 11-8 on page 11-26, but s2 has SHARED8 field alignment and includes filler
items where required.
Structure s2 in Example 11-9 on page 11-26 uses 14 more bytes than structure s1 in
Example 11-8 on page 11-26. If the order of the fields within the structure is not
important, you can rearrange the fields so that the structure contains fewer bytes, as in
Example 11-10
on page 11-26.
Example 11-8. FIELDALIGN(SHARED2) Structure
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; ! s2 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
Example 11-9. FIELDALIGN(SHARED8) Structure
STRUCT s2 FIELDALIGN(SHARED8);
BEGIN
INT i; ! i begins at offset: 0 bytes
FILLER 2; ! Two bytes of filler
INT(32) j; ! j begins at offset: 4 bytes
STRING s1; ! s1 begins at offset: 8 bytes
STRING s2; ! s2 begins at offset: 9 bytes
FILLER 6; ! 6 bytes of filler
FIXED f; ! f begins at offset: 16 bytes
INT k; ! k begins at offset: 24 bytes
FILLER 6; ! Pad to a multiple of widest field (f)
END; ! Total length of s2: 32 bytes
Example 11-10. Rearranged FIELDALIGN(SHARED8) Structure
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 widest field (f)
END; ! Total length of s3: 24 bytes