Solaris SPARC to Solaris x86 Porting Guide

return (date_to_compare.month - date_compare_against.month);
if (date_to_compare.day != date_compare_against.day)
return (date_to_compare.day - date_compare_against.day);
}
Data alignment differences
The alignment of the field members in a structure differs across platforms, resulting in variable
padding requirements and causing the structure to be a different size. To make the code portable, use
the sizeof operator to access the structure size.
Read/-write structures
Most programs read and write data to the permanent storage media such as a complete structure in
binary form using standard C routines. These routines need the data size that is being read or written.
Due to the different alignment and data type sizes on different platforms, the structure sizes vary. Use
the sizeof operator to specify the number of bytes to read or write in these routines.
For example, if you have a program that must read a record of the type MyRecord, which has a total
of 25 bytes of data, do not write the following:
MyRecord myrecord;
fread(&myrecord, 25, 1, fp);
Instead, use the following convention:
MyRecord myrecord;
fread(&myrecord, sizeof(MyRecord), 1, fp);
Alignment of double and long double
Differences in alignment of double and long double floating-point variables might cause porting
issues. The SPARC processor enforces 8-byte alignment on double float variables, while the x86
processor enforces only 4-byte alignment.
The following example demonstrates byte alignment issues:
#include <stdio.h>
main()
{
unsigned char bbb[5] = {0x12, 0x34, 0x56, 0x78, 0x9a };
printf("%x", *(int *)(bbb));
printf("%x", *(int *)(bbb + 1));
exit(0);
}
This is an example of a poorly written, non-portable C program. This code might be made to run on
SPARC processors using either the -misalign and -misalign2 compiler options, but not on an
x86 operating system.
Padding
The alignment of the field members in a structure differs across platforms, resulting in variable
padding requirements, and causing the structure to be a different size. The sizeof (BITMAP), structure,
shown in the following example, might not be the same on different platforms:
typedef struct bitmap {
WORD word_type;
DWORD dword_type;
} BITMAP;
7