Path: utzoo!utgpu!water!watmath!clyde!rutgers!sri-unix!quintus!ok From: ok@quintus.UUCP (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: data alignment (was Re: RISC data alignment, in comp.arch) Message-ID: <661@cresswell.quintus.UUCP> Date: 18 Feb 88 01:58:20 GMT References: <2635@calmasd.GE.COM> <28200092@ccvaxa> <496@ecrcvax.UUCP> <2309@umd5.umd.edu> Organization: Quintus Computer Systems, Mountain View, CA Lines: 32 In article <2309@umd5.umd.edu>, chris@trantor.umd.edu (Chris Torek) writes: > >In article <28200092@ccvaxa> aglew@ccvaxa.UUCP writes: > >>I have often wanted an optimizing compiler that could [rearrange] > >>struct { long a; int b; long c; char d; } > >>... and make it into AAAACCCCBBDx. > Yes. In fact, I have the perfect name for it, stolen from Craig > Stanfill's thesis hacking days here at U of MD. ... Hence the > proper name for an unordered aggregate type: a `bag'. > Please don't do that. "bag" is a very common name for the "multiset" data type. (That is, a homogeneous collection like a set, except that items can be present more than once.) There is a trivial solution, and a non-trivial solution. If you put all doubles, arrays of doubles, or structs with double elements first, then all floats, arrays of floats, or structs with float elements, then all pointers, arrays of pointers, or structs with pointer elements, then all longs, arrays of longs, or structs with long elements, then all ints, arrays of ints, or structs with int elements, then all shorts, arrays of shorts, or structs with short elements, then all chars, arrays of chars, or structs with char elements the resulting 'struct' will be reasonably packed on a wide range of machines. That is the trivial solution. The non-trivial solution is to write your structure declaration in two steps. Suppose you have a collection of typedefs and macros in a file header.h, and a collection of struct declarations you would like to optimise. Now write another program which does includes header.h and uses the sizeof() information to determinate an appropriate order for each struct, and have that program write out a structs.h file with the optimised declarations.