09.06.2013 Views

Intel XENIX 286 Programmers Guide (86) - Tenox.tc

Intel XENIX 286 Programmers Guide (86) - Tenox.tc

Intel XENIX 286 Programmers Guide (86) - Tenox.tc

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>XENIX</strong> Programming C Language Portability<br />

Bitfields<br />

Bitfields are not implemented in all C compilers. When they are, no field may be larger<br />

than an int, and no field can overlap an int boundary. If necessary the compiler will<br />

leave gaps and move to the next int boundary.<br />

The C language makes no guarantees about whether fields are assigned left to right, or<br />

right to left in an int. Thus, while bitfields may be useful for storing flags and other<br />

small data items, their use in unions to dissect bits from other data is definitely<br />

nonportable.<br />

To ensure portability, no individual field should exceed 16 bits.<br />

Pointers<br />

The C language is fairly generous in allowing manipulation of pointers, to the extent<br />

that most compilers will not object to nonportable pointer operations. The lint program<br />

is particularly useful for detecting questionable pointer assignments and comparisons.<br />

The common nonportable use of pointers is the use of casts to assign one pointer to<br />

another pointer of a different data type. This almost always makes some assumption<br />

about the internal byte ordering and layout of the data type, and is therefore<br />

nonportable. In the following code, the byte order in the given array is not portable:<br />

char c[4];<br />

long *lp;<br />

lp = (long *)&c[O];<br />

*lp = Ox 12345678L;<br />

The lint program will issue warning messages about such uses of pointers. Code like this<br />

is very rarely necessary or valid. It is acceptable, however, when using the malloc<br />

function to allocate space for variables that do not have char type. The routine is<br />

declared as type char * and the return value is cast to the type to be stored in the<br />

allocated memory. If this type is not char * then lint will issue a warning concerning<br />

illegal type conversion. In addition, the malloc function is written to always return a<br />

starting address suitable for storing all types of data. lint does not know this, so it gives<br />

a warning about possible data alignment problems, too. In the following example,<br />

malloc is used to obtain memory for an array of 50 integers.<br />

extern char *malloc();<br />

int *ip;<br />

ip = (int *)malloc(SO);<br />

This example will cause a warning message from lint.<br />

The C reference manual (contained in The C Programming Language by Kernighan and<br />

Ri<strong>tc</strong>hie} states that a pointer can be assigned (or cast} to an integer large enough to hold<br />

it. Note that the size of the int type depends on the given machine and implem entation.<br />

This type is long on some machines and short on others. In general, do not assume that<br />

sizeof(char *) == sizeof(int).<br />

A-5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!