[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: int vs. struct



 I should point out that in C, any "standard interface" based on structs
is fraught with peril unless it is rigourously adhered to, and that if
the interface includes a networking substrate, it is essential that the 
exact byte sequences be specified.

For example:

	struct point { short x;short y; };

will, in byte terms, store x and y in multiple different ways, depending
on the endian class of the machine, and on the whim of the C compiler.

defining
	#define x_from_point(i)	((short)(i>>16))
	#define y_from_point(i)	((short)(i&0xffff))
	#define point(x,y)		((x<<16)|(y&0xffff))

is safe locally

both possible definitions will be non-portable across networks and file
systems unless the protocol for transmitting a point is specified (and
adhered to) as a sequence of bytes.  For example, writing an array of 
"points", or a "point" embedded in a larger struct, will not be portable 
using simple code such as:

	write(stream,&a_point,4);