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

  • Home
  • SEARCH
  • 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

Related Questions

Consider the following code: #include <stdio.h> int main(void) { int a[10]; printf(%d,(int)sizeof(a)); //prints 10*sizeof(int)
here is code #include <iostream> #include <map> using namespace std; int main(){ map<int ,int>a;
just for testing i had created the following code: #include<stdio.h> int main(){ char *p
What will be the output of the following code? #include<stdio.h> int main() { int
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
Consider the following code: #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <algorithm>
I have the following C-code: #include<stdio.h> #include<stdlib.h> typedef struct node { int a; }node;
gcc 4.4.1 c89 I have the following code snippet: #include <stdlib.h> #include <stdio.h> char
The following code uses getchar() to accept a line of input. #include <stdio.h> #include
The following code causes a SIGSEGV, but only while debugging. #include <stdio.h> #include <stdlib.h>

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.