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

Table Of Contents
Handling TNS Data Alignment
HP C/C++ Programmer’s Guide for NonStop Systems429301-010
23-8
C/C++ Misalignment Examples
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;
}
Example 23-2. C/C++ Invalid Cast From char to Integer Pointer (Item 2, Item 10)
Change this:
/* extract 16-bit length from front of long string: */
unsigned char *name;
lth = * (unsigned short *) name; /* misalignment traps here */
name += 2;
To this:
/* extract 16-bit length from front of long string: */
unsigned char *name;
lth = (name[0] << 8) | name[1]
name += 2;