Re: ClassDef() Increases class size? + a Question to CINT gurus

John Zweizig (jzweizig@ligo.caltech.edu)
Thu, 20 May 1999 16:06:02 -0700 (PDT)


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).