--- In [email protected], Thomas Hruska <thru...@...> wrote: > ~Rick wrote: > > On Dec 24, 2009, at 1:40, "Peter Nilsson" <peternilsso...@...> > > wrote: > > > union myarray > > > { > > > unsigned char as_char[128]; > > > short as_short[128 / sizeof(short)]; > > > int as_int[128 / sizeof(int)]; > > > } myarray; > > > ... > > > > I've always ordered unions from the largest sized elements > > tithe smallest in order to avoid alignment issues
That practice is often used with structs to reduce padding required because of alignment issues. > > -- or, align the entire union on a specific boundary. > > Is that not necessary? Unions and structs will always be padded if necessary to ensure that an array of such will be properly aligned for each element and each member therein. > Not necessary. A union is the size of the largest element. Or it may be larger. Both structs and unions may be padded. IIRC, the old Mac OSes supported 12 byte (software) floating point types as well as a natural 8 byte type. It is conceivable that a compiler may enforce 8 byte alignment if both types are used in a struct or union, thus making a union at least 16 bytes if both are included. -- Peter
