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 7541199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:49:59+00:00 2026-05-30T07:49:59+00:00

This is an online C++ test question, which has been done. #include<iostream> using namespace

  • 0

This is an online C++ test question, which has been done.

#include<iostream>
using namespace std; 
class A
{

};
class B
{
int i; 
}; 

class C
{
void foo();
};
class D
{
virtual void foo();
};

class E
{
int i ; 
    virtual void foo();
};
class F
{
int i; 
    void foo();
};
class G
{
    void foo();
    int i;
    void foo1();
};

class H
{
    int i ;
    virtual void foo();
    virtual void foo1();
};
int main()
{
cout <<"sizeof(class A) : " << sizeof(A) << endl ;
cout <<"sizeof(class B) adding the member int i : " << sizeof(B) << endl ;
cout <<"sizeof(class C) adding the member void foo() : " << sizeof(C) << endl ;
cout <<"sizeof(class D) after making foo virtual : " << sizeof(D) << endl ;
cout <<"sizeof(class E) after adding foo virtual , int : " << sizeof(E) << endl ;
cout <<"sizeof(class F) after adding foo  , int : " << sizeof(F) << endl ;
cout <<"sizeof(class G) after adding foo  , int : " << sizeof(G) << endl ;
G g;
cout <<"sizeof(class G) after adding foo  , int : " << sizeof(g) << endl ;
cout <<"sizeof(class H) after adding int 2 virtual " << sizeof(H) << endl ;
return 0; 
}

output:

sizeof(class A) : 1
sizeof(class B) adding the member int i : 4
sizeof(class C) adding the member void foo() : 1
sizeof(class D) after making foo virtual : 8
sizeof(class E) after adding foo virtual , int : 16
sizeof(class F) after adding foo  , int : 4
sizeof(class G) after adding foo   , unsigned int : 4
sizeof(class g) after adding foo  , unsigned int : 4
sizeof(class H) after adding int 2 virtual 16

My questions:

Why siszeof(A) is 1 and sizeof(C) is 1 too ?

Why siszeof(H) is 16 but sizeof(G) is 4 ?

Why siszeof(E) is 16 but sizeof(F) is 4 ?

Why siszeof(D) is 8 but sizeof(E) is 16 ?

My guess:

A virtual function is a pointer with 8 bytes.
But, I do not know why E size is 16 ?
Adding a function to an empty class does not change its size ?

Any help is appreciated.

thanks

  • 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. Editorial Team
    Editorial Team
    2026-05-30T07:50:00+00:00Added an answer on May 30, 2026 at 7:50 am

    First off, a virtual function is not a pointer with 8 bytes. In C++ nothing but sizeof(char) is guaranteed to be any number of bytes.

    Second, only the first virtual function in a class increases its size (compiler-dependent, but on most – if not all – it’s like this). All subsequent methods do not. Non-virtual functions do not affect the class’s size.

    This happens because a class instance doesn’t hold pointers to methods themselves, but to a virtual function table, which is one per class.

    So if you had:

    class A
    {
       virtual void foo();
    }
    

    and

    class B
    {
       virtual void goo();
       virtual void test();
       static void m();
       void x();
    }
    

    you would have sizeof(A) == sizeof(B).

    And now:

    Why siszeof(A) is 1 and sizeof(C) is 1 too ?

    A and C have size 1 just because it’s not allowed for a class to be of size 0. The functions have nothing to do with it. It’s just a dummy byte.

    Why siszeof(H) is 16 but sizeof(G) is 4 ?

    G has only one member that accounts for memory – the int. And on your platform, sizeof(int) == 4. H, besides the int, also has a pointer to the vftable (virtual function table, see above). The size of this, size of int and allignment are compiler specific.

    Why siszeof(E) is 16 but sizeof(F) is 4 ?

    Explained above – non virtual methods don’t take up memory in the class.

    Why siszeof(D) is 8 but sizeof(E) is 16 ?

    D only contains the vftable pointer which is apparently 8 bytes on your platform. E also has an int, and the vftable is aligned to 8 bytes. So it’s something like:

    class E
    
    4 bytes for int |  4 padding bytes  |  8 bytes for vftable pointer  | 
    | x | x | x | x |    |    |    |    | v | v | v | v | v | v | v | v |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked before, but I have tried the given
Been trying to find this online for a while now. I have a SDL_Surface
I found this open question online. How do you process a large data file
This is something that's been bugging me for many years: why most online services
This is such a difficult question to explain online but I shall try my
We have some client code which is using the SqlConnection class in .NET to
I'm pretty new to php so this might be a noob question. I'm using
I had no luck with this question so I've produced this simple-as-possible-test-case to demonstrate
Hey, I'm developing an online test using servlets/jsps and was wondering how to create
I'm working on a large SQL Server codebase, some of which has been in

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.