[net.unix-wizards] Size of struct not same on Ultrix as on v7

das@cirl.UUCP (Dave Steffens) (03/25/86)

Background: We would like to generate data files on a PDP11/34 (running v7)
and ship them to our uVAX II (running Ultrix) for further processing.
The data files are created by writing out records built up from struct's.
The program which processes the files expects to be able to read records
using the same structure definitions which were used to create them.

We are thus in the process of porting programs written on v7 to Ultrix.
One major problem has been that struct's appear not to be the same size
when compiled on Ultrix as they are when compiled on v7.
This appears to be true for 4.2bsd as well.

What seems to be happening is that under Ultrix (and 4.2bsd) the compiled
length of every struct is rounded up to a multiple of the length of the
longest element in it.  Thus, struct's containing neither ints nor longs are
*always* multiples of 2 bytes whereas struct's containing either ints or longs
are *always* multiples of 4 bytes.

The bottom line is that files written on v7 can be processed correctly on v7
and files written on Ultrix can be processed correctly on Ultrix
but files written on v7 can't be processed correctly on Ultrix because
the *padded* struct's on Ultrix do not match the record size as written on v7.

Why does the Ultrix (4.2bsd) C compiler insist on padding struct's?
Is there any fix (other than hard-wiring the record size into every program)?
-- 
{harvard,mit-eddie,think}!eplunix!earvax!das	David Allan Steffens
243 Charles St., Boston, MA 02114		Eaton-Peabody Laboratory
(617) 523-7900 x2748				Mass. Eye & Ear Infirmary

gwyn@brl-smoke.ARPA (Doug Gwyn ) (03/25/86)

The VAX PCC tries to align data more stringently than necessary
(which would be on arbitrary byte boundaries) because its implementor
was trying to accommodate the way the VAX-11/780 memory system
operated, in order to speed up memory references somewhat.

To write binary data on a PDP-11 for export to a VAX-11:
	struct	{
		char	c1;
		char	pad1[3];	/* alignment padding */
		float	f;
		long	l;
		short	s;
		char	pad2[2];	/* alignment padding */
		double	d;
		char	c2;
		char	pad3[3];	/* alignment padding */
		}	record;
In other words, include sufficient padding in your struct so
that the compilers on both systems will not add any more padding.
Also be aware that you have to swap the halves of a (long) when
moving it between a PDP-11 and a VAX-11.  Finally, if you have
to move the data to another architecture, some or all of the
binary information (particularly floating-point) would have to
be interpreted for the target.  This is why it is recommended
that inter-machine transfer of data be done in character format
when feasible.

chris@umcp-cs.UUCP (Chris Torek) (03/26/86)

In article <258@cirl.UUCP> das@cirl.UUCP writes:

>... We are thus in the process of porting programs written on v7
>to Ultrix.  One major problem has been that struct's appear not to
>be the same size when compiled on Ultrix as they are when compiled
>on v7.  This appears to be true for 4.2bsd as well.

Welcome to the wonderful world of heterogeneity.  No one ever said
your structures would look the same.  If you want the data file to
contain a machine independent byte stream, you have to read and
write a machine independent byte stream.  There is no reliable
alternative.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1415)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@mimsy.umd.edu