Path: utzoo!mnetor!uunet!mcvax!unido!ecrcvax!johng From: johng@ecrcvax.UUCP (John Gregor) Newsgroups: comp.arch Subject: Re: RISC data alignment Message-ID: <504@ecrcvax.UUCP> Date: 22 Feb 88 11:31:50 GMT References: <2635@calmasd.GE.COM> <28200092@ccvaxa> <496@ecrcvax.UUCP> <3001@bloom-beacon.MIT.EDU> Reply-To: johng@ecrcvax.UUCP (John Gregor) Organization: ECRC, Munich 81, West Germany Lines: 54 In article <3001@bloom-beacon.MIT.EDU> jfc@athena.mit.edu (John F Carr) writes: >In article <496@ecrcvax.UUCP> johng@ecrcvax.UUCP (John Gregor) writes: >>In article <28200092@ccvaxa> aglew@ccvaxa.UUCP writes: >}}I have often wanted an optimizing compiler that could take >}}struct { long a; int b; long c; char d; } >}}which is usually mapped into bytes as AAAABBxxCCCCDxxx >}}(on a machine where misaligned accesses are penalized) >}}and make it into AAAACCCCBBDx. >}I would like that also, but don't call it a struct. Rearranging the order >}breaks lots of code and violates K&R p. 196. >Isn't code which depends on allignment and spacing risky anyway? >If a program assumes that for >struct { int a; > int b; > } s; > (&(s.a) - &(s.b)) == 1 it is making an unsupported assumption. Can >someone post an example of a program which is portable and depends on >the K&R rule quoted above? > --John Carr (jfc@ATHENA.MIT.EDU) That's not a good example of the type of code that would break. I use the ordering property of structs to hide data. If I have a struct which has some house keeping at the end of the struct, I don't have to tell the rest of the modules about it. e.g. struct private_foo_t { int a; short b; /* would be reordered to AAAACCCCBB */ int c; } fooba; struct public_foo_t { int a; short b; } foobr; By K&R I am guaranteed that: (&(fooba.b) - &(fooba.a)) == (&(foobr.b) - &(foobr.a)) /* i.e. the offsets are identical as long as the types ** are the same. */ This would break if structs were reordered. I believe that C++ uses this feature of C to implement its data hiding also. I do like the idea of an unordered aggregate type however. I've had to reorder structs for memory usage too many times and it's very machine dependent. -- pqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpqpq bdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbd John Gregor johng%ecrcvax.UUCP@germany.CSNET