C/C++ Programmer's Guide (G06.27+, H06.08+, J06.03+)

5. 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:
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 contains some noncharacter fields and has an odd number
of bytes, to give it an even number of bytes
See Example 14: C/C++ Structure With Implicit Filler Bytes (Item 5).
To prevent this problem, use an offsetof() macro to get the offset of a structure field and
the sizeof instruction to get the size of a structure.
6. Customizing the heap allocation method
If a TNS program implements its own customized heap allocation method, it must ensure that
all objects except char objects are aligned and allocated on even-byte boundaries.
7. Appending a sequence of objects into a string buffer or char array
If a TNS program appends a sequence of objects into a string buffer or a char array, it must
ensure that nonstring objects are aligned and allocated on even-byte boundaries.
8. Addressing an external data item that contains misaligned parts (as short or larger items)
If a TNS program uses external data items (files or structures) that have misaligned parts (such
as those on computer systems that have no data alignment requirements), it must declare and
directly access them as char arrays rather than as short or larger items. See Example 15:
C/C++ External Structure With Misaligned Parts (Item 8).
9. Using string check-sum or hashing functions that use 16-bit or 32-bit ADD or XOR operations
Some TNS programs use check-sum or hashing functions that use a series of 16-bit or 32-bit
ADD or XOR operations to reduce an arbitrary length byte string to 16 or 32 bits. If the string
begins on an even-byte boundary, this is often coded as an array of short or int elements
overlaying the byte string, but if the string can begin at an arbitrary address, the loop must
not use short * or int * pointers. See Example 16: C/C++ Check-Sum Function (Item 2,
Item 9).
10. Storing the length of a byte string in its first two bytes
If the string can begin at an arbitrary address, the two bytes in which the length is stored must
be stored and accessed as separate bytes, not as a single short integer. See:
Example 11: C/C++ Invalid Cast From char to Integer Pointer (Item 2, Item 10)
Example 12: C/C++ Invalid Cast From char to Integer Pointer (Item 2, Item 10)
Example 13: C/C++ Pointer Union (Item 3, Item 10)
11. Ending a byte string with multiple zero-filled bytes
The zero-filled bytes must be inserted by separate single-byte storage operations, not by a
single short storage operation.
Example 10 C/C++ Null Pointer Check (Item 1b)
Change this:
struct listnode {
int *kind;
...
struct listnode *next;
};
struct listnode *listhead, *node;
C/C++ Misalignment Examples 387