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 23:07:28 PST Path: bga.com!news.sprintlink.net!howland.reston.ans.net!usc!bloom-beacon.mit.edu!crl.dec.com!crl.dec.com!nntpd.lkg.dec.com!usenet From: Tim Bartley Newsgroups: comp.unix.osf.osf1 Subject: Re: Funny alignment with structures ? Date: 7 Dec 1994 06:56:33 GMT Organization: Digital Equipment Corporation Message-ID: <3c3mb1$e64@nntpd.lkg.dec.com> References: <3c1slb$qb3@newsstand.cit.cornell.edu> <3c2dij$268@nntpd2.cxo.dec.com> NNTP-Posting-Host: matfin.ozy.dec.com alan@nabeth.cxo.dec.com writes > 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 ; > } > You can also use the following construct: #pragma member_alignment save #pragma nomember_alignment struct { [definitions for struct you want no padding for] }; #pragma member_alignment restore if you use the -migrate option on the C compiler. This gives you access to the DEC C compiler (you may need to have the CMPDEVENH200 subset installed). Check out the cc(1) man page for more information on the -migrate switch. The compiler will then generate the appropriate instruction sequences so that unaligned accessed do not occur. The advantage of this approach is that you can control the padding on a structure by structure basis. Hope this helps, Tim.