From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,27231e56b1afd21f X-Google-Attributes: gidfee84,public X-Google-ArrivalTime: 1994-12-06 11:57:53 PST Path: bga.com!news.sprintlink.net!howland.reston.ans.net!europa.eng.gtefsd.com!news.mathworks.com!usenet.eel.ufl.edu!usenet.cis.ufl.edu!caen!crl.dec.com!crl.dec.com!nntpd.lkg.dec.com!nntpd2.cxo.dec.com!nabeth.cxo.dec.com!alan From: alan@nabeth.cxo.dec.com (Alan Rollow - Dr. File System's Home for Wayward Inodes.) Newsgroups: comp.unix.osf.osf1 Subject: Re: Funny alignment with structures ? Date: 6 Dec 1994 19:20:51 GMT Organization: Digital Equipment Corporation Distribution: world Message-ID: <3c2dij$268@nntpd2.cxo.dec.com> References: <3c1slb$qb3@newsstand.cit.cornell.edu> Reply-To: alan@nabeth.cxo.dec.com (Alan Rollow - Dr. File System's Home for Wayward Inodes.) NNTP-Posting-Host: nabeth.cxo.dec.com In article <3c1slb$qb3@newsstand.cit.cornell.edu>, olin@cheme.cornell.edu (Steve Thompson) writes: > >> [ Example deleted, but it showed a structure having poorly designed alignment for the requires of most RISC systems. ] >>Could someone please tell me how I can get around this funny alignment >>arrangement ? > >Count the bytes: the U32 field has two U8 fields and eight U16 fields >before it. Thats 2*1 + 8*2 = 18 bytes. Now since 18 is not a multiple >of 4 (the size of a U32), you get padding. To expand... Most RISC systems, the Alpha being no exception, require particular alignment of data objects. Bytes can start on any byte boundary. Shorts must start on a 2 byte boundary, 32 bits in on 4 byte boundaries and longs on 8 byte boundaries. The compilers enforce this alignment, to ensure that no traps are required to realign the data at run-time. You may be able to override some of the realignment enforced by the compiler, using compiler options (-Zp looks likely for the OSF C compiler). But the hardware still won't like it and you're likely to get unaligned access trap messages. You can fix this by realigning the data by hand if stuck with that particular data format, but it will use some program time to do it. I use something like the following: convert(char *bytes) { union { char bytes[4] ; int word ; } tmp ; tmp.bytes[0] = bytes[0] ; tmp.bytes[1] = bytes[1] ; tmp.bytes[2] = bytes[2] ; tmp.bytes[3] = bytes[3] ; return tmp.word ; } If you know the incoming data alignment, you can adjust the algorithm accordingly; using two shorts instead of four bytes. > >-steve >/sig > >--------------------------------------------------------------------------- >Steve Thompson, System Mangler Internet: thompson@cheme.cornell.edu -- Alan Rollow alan@nabeth.cxo.dec.com