Open System Services Porting Guide (G06.24+, H06.03+)

Table Of Contents
Porting From Specific UNIX Systems
Open System Services Porting Guide520573-006
9-9
Using Data Overlays
The endian problem affects programs that are ported between platforms that use
different byte-endian ordering when those programs access data within a word by
using byte addresses or bit locations. For example:
If var1=256+32+3, its value is 291 or 0x00000123.
If the value of var1 is manipulated as a unit, then no problem exists. However, if the
value is manipulated by byte, then the most significant byte of var1 contains 23 on a
little-endian system but contains 00 on a big-endian system. This difference is
important in the areas discussed in the following subsections:
Using Data Overlays on page 9-9
Initializing Multiple-Word Entities in Chunks of Multiple Bits on page 9-10
Using Bytes for More Than One Purpose on page 9-10
Using Hexadecimal Constants as Byte Arrays on page 9-10
In general, these practices are discouraged in modern C programs and should not be
used in source code that you need to port. However, you should check for them.
Using Data Overlays
Data overlays are possible in many programming languages:
In C, type unions, type casting, and unbounded arrays can all cause data overlay.
In COBOL, the use of REDEFINES causes data overlay.
In TAL, the use of address equations, empty arrays, and unbounded arrays can all
cause data overlay.
For example, unions such as the following can result in an endian problem:
union int_byte {
int_val;
char byte[4];
};
union int_byte my_var;
my_var.int_val = 0x1000000;
if(my_var.byte[3] == 0)
printf("The number is divisible by 256\n");
On a big-endian system, this code works correctly; byte 3 is the least significant byte
and contains 0 when my_var.int_val contains hexadecimal 1000000 and when that
number is divided by decimal 256. However, on a little-endian system, byte 3 is the
most significant byte and contains 1 initially when my_var.int_val contains
hexadecimal 1000000, even though hexadecimal 1000000 is evenly divisible by
decimal 256.
To fix this problem, use code such as the following that is not sensitive to endian
issues:
if((my_var.int_val & 0xff) == 0)
printf("The number is divisible by 256\n");