From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6389 invoked by alias); 7 May 2004 19:39:31 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 6342 invoked from network); 7 May 2004 19:39:30 -0000 Received: from unknown (HELO monty-python.gnu.org) (199.232.76.173) by sourceware.org with SMTP; 7 May 2004 19:39:30 -0000 Received: from [80.91.224.249] (helo=main.gmane.org) by monty-python.gnu.org with esmtp (Exim 4.33) id 1BMAPm-0006V0-OS for gcc-help@gcc.gnu.org; Fri, 07 May 2004 14:49:43 -0400 Received: from list by main.gmane.org with local (Exim 3.35 #1 (Debian)) id 1BMAMd-0000rN-00 for ; Fri, 07 May 2004 20:46:28 +0200 Received: from pcd655155.netvigator.com ([218.102.187.155]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 07 May 2004 20:46:27 +0200 Received: from aditsu by pcd655155.netvigator.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 07 May 2004 20:46:27 +0200 To: gcc-help@gcc.gnu.org From: Adrian Sandor Subject: Re: sizeof with virtual inheritance Date: Fri, 07 May 2004 19:39:00 -0000 Message-ID: References: <6.0.3.0.2.20040427070648.01f97650@iplan-mn.corp.adobe.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: main.gmane.org User-Agent: Loom/3.14 (http://gmane.org/) X-Loom-IP: 218.102.187.155 (Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.6) Gecko/20040113) X-SW-Source: 2004-05/txt/msg00080.txt.bz2 Eljay Love-Jensen writes: > The structure has extra bookkeeping information to keep track. > > For example, if you have multiple inheritance of: > foo : bar > baz : bar > quux : foo, baz > > The memory layout of quux is going to be: > [...] Hi, sorry for the late reply Your comments apply generally to multiple inheritance, you didn't say anything about virtual inheritance. Besides, for non-virtual multiple inheritance, there seems to be no extra data in the objects. Here is a program that shows what I'm talking about: //--------8<--------- #include using namespace std; class c1{char x;}; class c2:public c1{}; class c3:public c1{}; class c4:virtual public c1{}; class c5:virtual public c1{}; class c6:public c2, public c3{}; class c7:public c4, public c5{}; #define S(x) cout<<"c"#x<<": "<8--------- I compiled it with mingw - gcc 3.2 (in Windows) and here are the results: c1: 1 c2: 1 c4: 8 c6: 2 c7: 12 I had the same results on a linux system too. As you can see, the only difference between c2 and c4 is the virtual inheritance, yet the size difference is 7 bytes (it probably has an extra 4-byte pointer aligned at a 4-byte offset). Classes c6 and c7 have multiple inheritance (basically they both inherit c1 twice, indirectly), and again the difference is virtual inheritance. c6 really gets 2 copies of c1, so the size is 2. But c7 should only get 1 copy of c1 because of the virtual inheritance; yet its size is 12, which suggests it has 2 extra pointers or something. I'd like to know what's that extra data used for and why it is necessary. I'd also like to know when and where it is added, because I haven't managed to find an algorithm to calculate the size based on the inheritance graph. Thanks Adrian