Newsgroups: comp.lang.c Path: utzoo!utgpu!jarvis.csri.toronto.edu!csri.toronto.edu!flaps From: flaps@csri.toronto.edu (Alan J Rosenthal) Subject: Re: Optimal structure field ordering Message-ID: <8807061759.AA01379@baldwin.csri.toronto.edu> Organization: University of Toronto References: <163@navtech.uucp> <2775@ttrdc.UUCP> <164@navtech.uucp> <5175@ihlpf.ATT.COM> Date: Wed, 6 Jul 88 12:39:02 EDT On optimizing space by ordering the struct fields by the sizes of the elements, nevin1@ihlpf.UUCP (00704a-Liber,N.J.) writes: >This is not (necessarily) true! For instance: > >sizeof(long) === 8 >sizeof(short) === 5 >sizeof(char) === 1 > >Also suppose that alignment for shorts and longs occurs on even addresses >(two byte boundaries), while chars can align on single byte boundaries. Aha! This is inconsistent. If shorts must be even-aligned, sizeof(short) would have to report 6, not 5. Consider an array 5 of short, which must have size 29 or 30 to allow alignment, and which also must have sizeof(array) == 5 * sizeof(short). Going through your example again, you will see that, if sizeof(short) is 6 and thus a wasted byte follows all shorts, it is still most space-efficient to put the chars after the shorts. ajr -- - Any questions? - Well, I thought I had some questions, but they turned out to be a trigraph.