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-5
C/C++ Misalignment Examples
In TNS mode and accelerated mode, the targets of C and C++ pointers (except
pointers to char items) must be aligned on 2-byte memory boundaries for correct
operation. The results of odd-byte addresses depend on the specific NonStop server
and the system configuration, but they might include erratic “rounding down” and
abnormal program termination.
In a TNS-compiled C or C++ program, some actions that can cause misaligned
addresses are:
1. Using a null or uninitialized pointer
When a structure pointer is null or uninitialized, the structure’s fields are random
bits, which could give a random misaligned address if a pointer field is
dereferenced. Examples:
a. Dereferencing a pointer-valued field of a structure when the structure pointer is
null
b. Checking for a null pointer after some but not all pointer references, instead of
before all pointer references (see Example 23-1 on page 23-8).
c. Dereferencing a pointer-valued member of a union when that alternative is not
active and initialized
2. Using syntactically correct but semantically incorrect and nonportable casts of
these types:
To an integer pointer
To a structure pointer
From a char * pointer
From a void * pointer
From an integer expression
(For information about writing portable programs, see Writing Portable Programs
on page 1-19.)
These casts work only in TNS programs when the converted address is correctly
aligned for integer or structure objects (that is, when it is an even-byte address).
These are correctly aligned (that is, they have even-byte addresses):
Compiler-allocated objects
Heap objects
These might be misaligned (that is, they might have odd-byte addresses):
Declared items
°
char strings
°
Elements of char arrays
°
char fields of structures