Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!rutgers!att!westmark!mole-end!mat From: mat@mole-end.UUCP (Mark A Terribile) Newsgroups: comp.lang.c++ Subject: Re: Byte padding question Summary: Eh? Message-ID: <442@mole-end.UUCP> Date: 21 Sep 90 06:50:06 GMT References: <440@mole-end.UUCP> <1023@tcs.tcs.com> Distribution: comp Organization: mole-end--private system. admin: mole-end!newtnews Lines: 41 > |> > I also noticed that in it's child class the > |> > extra byte generated for the empty base class was added as well as > a new char > |> > array of size 1. > |> [ more stuff deleted ] > |> The array in the derived class is *probably* there to force subsequent > |> data members back to the ``correct'' or machine optimal alignment. After > |> a non-array char member, the next member can be begun at an odd boundary > |> if the machine will support it. > Nah, that can't be it. The C compiler already does that for you. The > C++ compiler/translator should never have to do alignment explicitly. > George J Wu | gwu@tcs.com or ucbcad!tcs!gwu It may if it has to type pun one struct into another, which is exactly how it handles certain cases of inheritance. The C compiler must maintain consistant alignment within one struct; it is not guaranteed to set up two structs such that one can be punned into the other, at least not in the way that C++ does it. Cfront seems to be capable of generating a short struct for the base class, a longer struct that includes the short struct as its first member to represent a derived class, AND a longer struct whose initial members are synonyms for the short struct. I suspect that the easiest way to ensure that alignment is handled consistently on many different underlying C compilers is to put that array in, relying on the requirement that what follows the array have the machine's natural alignment (or however it's stated). (For a single char, that requirement does not hold.) cfront does some incredible stuff. Some of it is very clever, some of it is very dumb, and some of it is cleverness in one place to allow dumbness in another. I suspect that this is one of the latter. -- (This man's opinions are his own.) From mole-end Mark Terribile