0

Size of class is 8 bytes only which is the size of vitual pointer? and what about size of vtable which is in the class?

#include <iostream>
    using namespace std;
    class Base
    {    public:
         virtual void f1(){}
         virtual void f2(){}
    };
    class Derive: public Base
    {
        
        void f1(){}
        void f2(){}
    };
    int main()
    {
        cout<<sizeof(Base)<<endl;
    }
  • The vtable is stored per **class**, not per _instance_, because the instance points to the class's vtable. You measure the size of an _instance_. So, even if the vtable as implemented in your compiler does increase in size with number of functions, you wouldn't see that because you only measure the size of a pointer to it. – underscore_d Aug 24 '20 at 08:49
  • Does this answer your question? [how to determine sizeof class with virtual functions?](https://stackoverflow.com/questions/4766323/how-to-determine-sizeof-class-with-virtual-functions) – underscore_d Aug 24 '20 at 08:51
  • @underscore_d: Theoretically, for a class with a single virtual function, it might make sense to just store the pointer to that function as a per-instance vtable. – einpoklum Aug 24 '20 at 08:53
  • Anyway, I think as a currently unclear/vague question without more specific focus, it's a duplicate, e.g. of [How are virtual functions and vtable implemented?](https://stackoverflow.com/questions/99297/how-are-virtual-functions-and-vtable-implemented) – underscore_d Aug 24 '20 at 08:56
  • If you have access to visual studio compiler, you can play with the options `/d1reportSingleClassLayout(+MyClassName)` and `/d1reportAllClassLayout`. It gives you some insights on the what is generated. – nop666 Aug 24 '20 at 09:01

3 Answers3

3

You'll probably find that if your implementation does use v-tables in order to implement virtual functions, then the class itself stores a pointer to the v-table.

The size of the pointer will not depend on the number of functions in the v-table to which it points.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

As an extension to Bathshebas answer (it is most of the answer), I would like to stress that what you get as sizeof(class), is the size of what needs storing per instance of the class, i.e. for each actually created object.
Whatever only needs storing once (for all the class) will not increase the size.
(And see Bathshebas answer concerning why the methods might be part of that.)

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
-1
      **I came across this conjecture ,since vtable is also static array of funtion pointers it is not tied to specific object , as in this scenario***************
      #include <iostream> 
        using namespace std; 
        class Base {
            public: 
         static int a[10];
         int *r=a; }; 
         
        int main() 
        { cout<<sizeof(Base)<<endl; }
    
 /// 8 bytes which is the size of the pointer
    
    
 Is it the correct notion??
  • 1
    Is this a question in wrong formatting, hidden in an answer to your own question? Please edit your question (by clicking the small grey word "edit" beneath your question) if you can add helpful details. If you have a different, though related question, please ask a new one by clicking the "Ask Question" button at the top right of this page. – Yunnosch Aug 27 '20 at 07:30
  • If this is supposed to be an answer to your own question it's not a very good answer. It doesn't explain _anything_. – Ted Lyngmo Nov 05 '20 at 08:50