TAL Programmer's Guide Data Alignment Addendum
Table Of Contents

TAL Misalignment Examples
TAL Programmer's Guide Data Alignment Addendum—524967-003
4-7
Incorrect Layout
Incorrect Layout
The TAL compiler aligns most declared objects on even-byte boundaries. The
exceptions are:
•
String elements
•
String fields of structures
•
Substructures containing only string elements
Examples of coding errors that cause incorrect layout are:
1. Using an explicit number for the offset of a structure field or for the size of a
structure
This practice can cause misaligned addresses by overlooking the “implicit” filler
bytes that the TNS compiler adds to structures as follows:
°
Within structures, to ensure that every noncharacter field begins at an even-
byte offset from the beginning of the structure
°
At the end of any structure that has an odd number of bytes, to give it an even
number of bytes
See Example 4-9 on page 4-8.
To prevent this problem, use the $OFFSET function to get the offset of a structure
field and the $LEN function to get the size of a structure.
2. Customizing the heap allocation method
If a TNS program implements its own customized heap allocation method, it must
ensure that nonstring objects are aligned and allocated on even-byte boundaries.
Example 4-8. TAL Code That Depends on Rounded-Down Addresses (Item 8)
Change this:
int proc extract(bitvect,n); ! extract nth bit of bit-vector
int .ext bitvect;
int n;
begin
int .ext p := @bitvect + $dbl(n/8); ! oops, creates odd address
! above stmt gives the intended result, namely
! p := @bitvect[n/16]
! only if odd address is implicitly rounded down when used
return (p '<<' (n land 15)) '>>' 15;
end;
To this:
int proc extract(bitvect,n); ! extract nth bit of bit-vector
int .ext bitvect;
int n;
begin
return (bitvect[n >> 4] '<<' (n land 15)) '>>' 15;
end;