Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 104261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:15:14+00:00 2026-05-11T01:15:14+00:00

The following code prints 20, i.e. sizeof(z) is 20. #include <iostream.h> class Base {

  • 0

The following code prints 20, i.e. sizeof(z) is 20.

#include <iostream.h> class Base {       public:             int a; };  class X:virtual public Base {       public:             int x; };  class Y:virtual public Base {       public:             int y; };  class Z:public X,public Y { };  int main() { Z z; cout << sizeof(z) <<endl; } 

Whereas if I don’t use virtual base classes here, i.e. for the following code : sizeof(z) is 16.

#include <iostream.h> class Base {       public:             int a; };  class X:public Base {       public:             int x; };  class Y:public Base {       public:             int y; };  class Z:public X,public Y { };  int main() { Z z; cout << sizeof(z) <<endl; } 

Why is sizeof(z) more(20) in the first case? Shouldn’t it be 12, since Base will be included only once in Z?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:15:14+00:00Added an answer on May 11, 2026 at 1:15 am

    Let’s look at the class layout of the two cases.

    Without the virtual, you have two base classes (‘X’ and ‘Y’) with an integer each, and each of those classes have integrated into them a ‘Base’ base class which also has an integer. That is 4 integers, 32-bits each, totalling your 16 bytes.

    Offset  Size  Type  Scope  Name      0     4   int   Base     a      4     4   int      X     x      8     4   int   Base     a     12     4   int      Y     y     16 size (Z members would come at the end) 

    (Edit: I’ve written a program in DJGPP to get the layout and tweaked the table to account for it.)

    Now let’s talk about virtual base classes: they replace the actual instance of the class with a pointer to a shared instance. Your ‘Z’ class has only one ‘Base’ class, and both instances of ‘X’ and ‘Y’ point to it. Therefore, you have integers in X, Y, and Z, but you only have the one Z. That means you have three integers, or 12 bytes. But X and Y also have a pointer to the shared Z (otherwise they wouldn’t know where to find it). On a 32-bit machine two pointers will add an additional 8 bytes. This totals the 20 that you see. The memory layout might look something like this (I haven’t verified it… the ARM has an example where the ordering is X, Y, Z, then Base):

    Offset  Size        Type  Scope  Name  Value (sort of)      0     4 Base offset      X     ?  16 (or ptr to vtable)      4     4         int      X     x      8     4 Base offset      Y     ?  16 (or ptr to vtable)     12     4         int      Y     y     16     4         int   Base     a     20 size (Z members would come before the Base) 

    So the memory difference is a combination of two things: one less integer and two more pointers. Contrary to another answer, I don’t believe vtables pay any (edit) direct (/edit) roll in this, since there are no virtual functions.

    Edit: ppinsider has provided more information on the gcc case, in which he demonstrates that gcc implements the pointer to the virtual base class by making use of an otherwise empty vtable (i.e., no virtual functions). That way, if there were virtual functions, it wouldn’t require an additional pointer in the class instance, requiring more memory. I suspect the downside is an additional indirection to get to the base class.

    We might expect all compilers to do this, but perhaps not. The ARM page 225 discusses virtual base classes without mentioning vtables. Page 235 specifically addresses ‘virtual base classes with virtual functions’ and has a diagram indicating a memory layout where there are pointers from the X and Y parts that are separate from the pointers to the vtable. I would advise anyone not to take for granted that the pointer to Base will be implemented in terms of a table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What do you mean when you say that 'the resize… May 11, 2026 at 11:40 pm
  • Editorial Team
    Editorial Team added an answer You're calling ToString on the MsgBox, not the Guid, which… May 11, 2026 at 11:40 pm
  • Editorial Team
    Editorial Team added an answer There are a few third-party extensions that will help you… May 11, 2026 at 11:40 pm

Related Questions

I have the following scrap of code to test MSMQ acknowledgments: static void Main(string[]
I am having problems with the greps in Emacs. a) grep doesnt seem to
Someone asked me a question via e-mail about integer partitions the other day (as
Gday All, I have been dabbling in some F# of late and I came

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.