Intel XENIX 286 Programmers Guide (86) - Tenox.tc
Intel XENIX 286 Programmers Guide (86) - Tenox.tc Intel XENIX 286 Programmers Guide (86) - Tenox.tc
XENIX Programming This is preferable to something like: #ifdef i8086 #define MAXPOS 32767 #else #end if C Language Portability To find the number of bytes in an int, use sizeof(int) rather than 2, 4, or some other nonportable constant. Storage Alignment The C language defines no particular layout for storage of data items relative to each other, or for storage of elements of structures or unions within the structure or union. Some CPUs, such as the PDP-11 and M68000, require that data types longer than one byte be aligned on even-byte address boundaries. Others, such as the 8086, 80286, and VAX-11 have no such hardware restriction. However, even with these machines, most compilers generate code that aligns words, structures, arrays, and long words on even addresses, or even long word addresses. Thus, on the VAX-11, the following code sequence gives 8, even though the VAX hardware can access an int (a 4-byte word) on any physical starting address: struct s tag { char c; int i'; }; printf("%d\n" ,sizeof(struct s _ tag) ); The principal implications of this variation in data storage are that data accessed as nonprimitive data types is not portable, and code that makes use of knowledge of the layout on a particular machine is not portable. Thus unions containing structures are nonportable if the union is used to access the same data in different ways. Unions are only likely to be portable if they are used simply to have different data in the same space at different times. For example, if the following union were used to obtain 4 bytes from a long word, the code would not be portable: union { char c[4]; long lw; } u; The sizeof operator should always be used when reading and writing structures: struct s _ tag st; write(fd, &st, sizeof(st) ); A-3
C Language Portability XENIX Programming This ensures portability of the source code. It does not produce a portable data file. Portability of data is discussed in a later section. Note that the sizeof operator returns the number of bytes an object would occupy in an array. Thus on machines where structures are always aligned to begin on a word boundary in memory, the sizeof operator will include any necessary padding for this in the return value, even if the padding occurs after all useful data in the structure. This occurs whether or not the argument is actually an array element. Byte Order in a Word The variation in byte order in a word affects the portability of data more than the portability of source code. However, any program that makes use of knowledge of the internal byte order in a word is not portable. For example, some XENIX/UNIX systems have an include file misc.h that contains the following structure declarat ion: I* * structure to access an * integer in bytes *I struct { char char }; lobyte; hi byte; With certain less restrictive compilers this could be used to access the high and low order bytes of an integer separately, and in a completely nonportable way. The correct way to do this is to use mask and shift operations to extract the required byte: #define LOBYTE(i) (i & Oxff) #define HIBYTE(i) ((i > > 8) & Oxff) Note that even this operation is only applicable to machines with two bytes in an int. One result of the byte ordering problem is that the following code sequence will not always perform as intended: int c = 0; read(fd, &c, 1); On machines where the low order byte is stored first, the value of c will be the byte value read. On other machines the byte is read into some byte other than the low order one, and the value of c is different. A-4
- Page 204 and 205: XENIX Programming yacc: Compiler-Co
- Page 206 and 207: XENIX Programming yacc: Compiler-Co
- Page 208 and 209: XENIX Programming yacc: Compiler-Co
- Page 210 and 211: XENIX Programming yacc: Compiler-Co
- Page 212 and 213: XENIX Programming yacc: Compiler-Co
- Page 214 and 215: XENIX Programming yacc: Compiler-Co
- Page 216 and 217: XENIX Programming yacc: Compiler-Co
- Page 218 and 219: XENIX Programming yacc: Compiler-Co
- Page 220 and 221: XENIX Programming yacc: Compiler-Co
- Page 222 and 223: XENIX Programming yacc: Compiler-Co
- Page 224 and 225: XENIX Programming yacc: Compiler-Co
- Page 226 and 227: XENIX Programming yacc: Compiler-Co
- Page 228 and 229: XENIX Programming yacc: Compiler-Co
- Page 230 and 231: XENIX Programming yacc: Compiler-Co
- Page 232 and 233: XENIX Programming yacc: Compiler-Co
- Page 234 and 235: XENIX Programming yacc: Compiler-Co
- Page 236 and 237: XENIX Programming %left %left %left
- Page 238 and 239: XENIX Programming I* lexical analys
- Page 240: XENIX Programming yacc: Compiler-Co
- Page 243 and 244: m4: Macro Processor XENIX Programmi
- Page 245 and 246: m4: Macro Processor XENIX Programmi
- Page 247 and 248: m4: Macro Processor XENIX Programm
- Page 249 and 250: m4: Macro Processor XENIX Programmi
- Page 251 and 252: m4: Macro Processor XENIX Programmi
- Page 253: C Language Portability XENIX Progra
- Page 257 and 258: C Language Portability XENIX Progra
- Page 259 and 260: C Language Portability XENIX Progra
- Page 261 and 262: C Language Portability XENIX Progra
- Page 263 and 264: C Language Portability XENIX Progra
- Page 266 and 267: APPENDIX B PROG RAMMING COMMANDS Th
- Page 268 and 269: XENIX Programming ad b (continued)
- Page 270 and 271: XENIX Programming Programming Comma
- Page 272 and 273: XENIX Programming Programming Comma
- Page 274 and 275: XENIX Programming Programming Comma
- Page 276 and 277: XENIX Programming Programming Comma
- Page 278 and 279: XENIX Programming Programming Comma
- Page 280 and 281: XENIX Programming Programming Comma
- Page 282 and 283: XENIX Programming Programming Comma
- Page 284 and 285: XENIX Programming as (continued) Fi
- Page 286 and 287: XENIX Programming Programming Comma
- Page 288 and 289: XENIX Programming Programming Comma
- Page 290 and 291: XENIX Programming Programm ing Comm
- Page 292 and 293: XENIX Programming Programming Comma
- Page 294 and 295: XENIX Programming Programming Comma
- Page 296 and 297: XENIX Programming Programm ing Comm
- Page 298 and 299: XENIX Programming Programming Comma
- Page 300 and 301: XENIX Programming Programming Comma
- Page 302 and 303: XENIX Programming Programming Comma
C Language Portability <strong>XENIX</strong> Programming<br />
This ensures portability of the source code. It does not produce a portable data file.<br />
Portability of data is discussed in a later section.<br />
Note that the sizeof operator returns the number of bytes an object would occupy in an<br />
array. Thus on machines where structures are always aligned to begin on a word<br />
boundary in memory, the sizeof operator will include any necessary padding for this in<br />
the return value, even if the padding occurs after all useful data in the structure. This<br />
occurs whether or not the argument is actually an array element.<br />
Byte Order in a Word<br />
The variation in byte order in a word affects the portability of data more than the<br />
portability of source code. However, any program that makes use of knowledge of the<br />
internal byte order in a word is not portable. For example, some <strong>XENIX</strong>/UNIX systems<br />
have an include file misc.h that contains the following structure declarat ion:<br />
I*<br />
* structure to access an<br />
* integer in bytes<br />
*I<br />
struct {<br />
char<br />
char<br />
};<br />
lobyte;<br />
hi byte;<br />
With certain less restrictive compilers this could be used to access the high and low<br />
order bytes of an integer separately, and in a completely nonportable way. The correct<br />
way to do this is to use mask and shift operations to extract the required byte:<br />
#define LOBYTE(i) (i & Oxff)<br />
#define HIBYTE(i) ((i > > 8) & Oxff)<br />
Note that even this operation is only applicable to machines with two bytes in an int.<br />
One result of the byte ordering problem is that the following code sequence will not<br />
always perform as intended:<br />
int c = 0;<br />
read(fd, &c, 1);<br />
On machines where the low order byte is stored first, the value of c will be the byte<br />
value read. On other machines the byte is read into some byte other than the low order<br />
one, and the value of c is different.<br />
A-4