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=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham 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!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newshosting.com!nx01.iad01.newshosting.com!uunet!ash.uu.net!spool.news.uu.net!not-for-mail Date: Thu, 01 Jul 2004 11:57:39 -0400 From: Hyman Rosen User-Agent: Mozilla Thunderbird 0.7.1 (Windows/20040626) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Zero length Objects References: <2oUEc.2$S77.1@nwrdny03.gnilink.net> <2kimpdF2orakU1@uni-berlin.de> In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Organization: KBC Financial Products Message-ID: <1088697459.558708@master.nyc.kbcfp.com> Cache-Post-Path: master.nyc.kbcfp.com!unknown@nightcrawler.nyc.kbcfp.com X-Cache: nntpcache 3.0.1 (see http://www.nntpcache.org/) NNTP-Posting-Host: 204.253.250.10 X-Trace: 1088697459 23324 204.253.250.10 Xref: g2news1.google.com comp.lang.ada:2032 Date: 2004-07-01T11:57:39-04:00 List-Id: 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.