Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!matrix!venkat From: venkat@matrix.UUCP (Desikan Venkatrangan) Newsgroups: comp.lang.c Subject: Re: RISC Machine Data Structure Word Alignment Problems? Keywords: risc sun Message-ID: <131@matrix.UUCP> Date: 1 Feb 90 17:20:32 GMT References: <111@melpar.UUCP> <1990Jan21.224826.1699@esegue.segue.boston.ma.us> Reply-To: venkat@matrix.UUCP (D. Venkatrangan) Organization: Matrix Computer Systems Inc. Lines: 55 In article <1990Jan21.224826.1699@esegue.segue.boston.ma.us> johnl@esegue.segue.boston.ma.us (John R. Levine) writes: >From article <111@melpar.UUCP>, by toppin@melpar.UUCP (Doug Toppin X2075): >> We are using the SUN 4/260 which is a RISC architecture machine. >> We are having trouble with data alignment in our data structures. and suggests: > You need to write something like this: > >read_foo_structure(struct foo *p) >{ > p->a = read_long(); > p->b = read_short(); > p->c = read_long(); >} > >long read_long(void) >{ > long v; > > /* read in big endian order */ > v = getc(f) << 24; /* should do some error checking */ > v |= getc(f) << 16; > v |= getc(f) << 8; > v |= getc(f); > return v; >} > >This may seem like more work, but in my experience you write a few of these >things and use them all over the place. Then your code is really portable. >-- For complete portablility and ease of maintenance, try to pattern such routines along the External Data Representation (XDR) as SUN has done, for support of the RPC mechanism. You will be writing bool_t xdr_foo(xdrs, objp) register XDR *xdrs; register objtype *opbjp; { return (xdr_long(xdrs, &objp->a) && xdr_short(xdrs, &objp->b) && xdr_long(xdrs, &obj->c)); } This way, both read and write operations can be done using the same routines; (with proper setting of XDR_ENCODE/XDR_DECODE in xdrs.) Also, the read/write can be from memory or a file. The xdr routines for the premitive types are provided by SUN. But they have chosen to represent 'shorts' as 4-byte quantities externally. If you wish to avoid this and prefer little-endian representation, you should write similar routines yourself. The utility rpcgen may be useful as well.