C/C++ Programmer's Guide (G06.25+)
Handling TNS Data Alignment
HP C/C++ Programmer’s Guide for NonStop Systems—429301-008
23-7
C/C++ Misalignment Examples
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 23-7 on page 23-9.
10. Storing the length of a byte string in its first two bytes
If the string can begin at an arbitrary address, then 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 23-2 on page 23-8
•
Example 23-3 on page 23-8
•
Example 23-4 on page 23-8
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 23-1. C/C++ Null Pointer Check (Item 1b)
Change this:
struct listnode {
int *kind;
...
struct listnode *next;
};
struct listnode *listhead, *node;
node = listhead;
while (*node->kind == 4 /* used too soon */ && node != NULL) {
...
node = node->next;
}
To this:
struct listnode {
int *kind;
...
struct listnode *next;
};
struct listnode *listhead, *node;
node = listhead;
while (node != NULL && *node->kind == 4) {
...
node = node->next;
}