OK, you are right; there's no complier that gives a struct size of 3 but the point is that ROOT/CINT (always?) pads structs to multiples of 8 Here is a quick comparison of gcc (on Sun/solaris) and RINT results of the testStruct.c file below: gcc: raphe:~/root>gcc testStruct.c -o testStruct raphe:~/root>testStruct sizeof(testByte) = 1 sizeof(testStruct) = 4 sizeof(testUnion ) = 4 sizeof(testByteArr[10]) = 10 sizeof(testStructArr[10]) = 40 sizeof(testUnionArr[10]) = 40 RINT: root [7] .L testStruct.c root [8] main() sizeof(testByte) = 8 sizeof(testStruct) = 8 sizeof(testUnion ) = 8 sizeof(testByteArr[10]) = 80 sizeof(testStructArr[10]) = 80 sizeof(testUnionArr[10]) = 80 file: testSruct.c ---------------------------------------------------------------- - #include <stdio.h> void main(){ struct Test0_t{ char c; }; struct Test0_t testByte; struct Test1_t{ char c; short i; }; struct Test1_t testStruct; union Test2_t{ char c[3]; short i; } testUnion; struct Test0_t testByteArr[10]; struct Test1_t testStructArr[10]; union Test2_t testUnionArr[10]; printf(" sizeof(testByte) = %d\n",sizeof(testByte)); printf(" sizeof(testStruct) = %d\n",sizeof(testStruct)); printf(" sizeof(testUnion ) = %d\n",sizeof(testUnion)); printf(" sizeof(testByteArr[10]) = %d\n",sizeof(testByteArr)); printf(" sizeof(testStructArr[10]) = %d\n",sizeof(testStructArr)); printf(" sizeof(testUnionArr[10]) = %d\n",sizeof(testUnionArr)); } ----- Original Message ----- From: John Zweizig <jzweizig@ligo.caltech.edu> To: Peter Lipa <lipa@nsma.arizona.edu> Cc: roottalk <roottalk@hpsalo.cern.ch> Sent: Thursday, May 20, 1999 4:06 PM Subject: Re: ClassDef() Increases class size? + a Question to CINT gurus > > > On Thu, 20 May 1999, Peter Lipa wrote: > > <snip> > > > > struct Test_t { > > Char_t c; > > Short_t i; > > } testStruct; > > > > Naively (and rightly so) you would expect to get a sizeof(testStruct) = 3. > > This you will get e.g. with the gcc compiler and default flags. > <snip> > > Your conclusions about the structure length being different than the sum > of the lengths of its constituent parts is of course correct, but I have > never seen a compiler that would allocate 3 bytes for testStruct. In > general (i.e. on all machines/compilers I have ever seen) the default > mapping of structures into memory follows three rules: > > 1) Structure elements are stored in memory in the order in which they > were defined. > > 2) Each atomic element (a simple variable or a member of a > sub-structure or array) is aligned on an offset that is a multiple > of its length. > > 3) The structure is padded to give a length that is a multiple of the > length of the longest atomic element. > > Thus for your example structure, ALL compilers I have ever seen > (including gcc) will give sizeof(struct testStruct) == 4 (assuming > sizeof(Short_t)==2 and sizeof(Char_t) == 1). > >
This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:33 MET