From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.4 required=5.0 tests=AC_FROM_MANY_DOTS,BAYES_00 autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,85c4b961f840b5ab X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!newsfeed.stanford.edu!cyclone.bc.net!in.100proofnews.com!in.100proofnews.com!Spring.edu.tw!news.nctu.edu.tw!feeder.seed.net.tw!attdv1!ip.att.net!newsfeed1.global.lmco.com!svlnews.lmms.lmco.com!not-for-mail From: "Xenos" Newsgroups: comp.lang.ada Subject: Re: Zero length Objects Date: Thu, 1 Jul 2004 12:05:52 -0400 Organization: Hades Message-ID: References: <2oUEc.2$S77.1@nwrdny03.gnilink.net> <2kimpdF2orakU1@uni-berlin.de> <1088697459.558708@master.nyc.kbcfp.com> NNTP-Posting-Host: 158.187.64.144 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 Xref: g2news1.google.com comp.lang.ada:2034 Date: 2004-07-01T12:05:52-04:00 List-Id: "Hyman Rosen" wrote in message news:1088697459.558708@master.nyc.kbcfp.com... > Xenos wrote: > > But C++ does compensate for this somewhat. Empty base classes (used for > > traits and the like) will not take any space in derived classes. For > > example: > > > > class Empty {}; > > class Derived : public Empty { char a; }; > > Derived D[1000000]; > > > > Here, Empty is not increase the size of D at all. > > But it's more complicated than that, because C++ does have the > similar rule that no distinct objects of the same type can have > the same address. Expanding on your example, > > struct Empty1 { }; struct Empty2 { }; > struct Derived : Empty1, Empty2 { char a; }; > Derived D[1000000]; > > The optimization still applies, and sizeof(Derived) > can still be one. But do as follows: > > struct Empty { }; > struct E1 : Empty { }; struct E2 : Empty { }; > struct Derived : E1, E2 { char a; }; > Derived D[1000000]; > > Now, Derived has two distinct subobjects both of type > Empty. They cannot be allocated at the same address, so > it's likely that sizeof(Derived) is > 1. Although, in > this case the compiler can get really clever and place > one of the Empty objects before the char and one after. > But cases can be concocted where extra pad bytes are > definitely required to avoid messing up object identity. > Which is why I said "somewhat."