CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    A question regarding the size of a class with virtual functions

    Here is an example,
    Code:
    class A
    {
    public:
    	virtual void foo(){}
    	virtual void foo2(){}
    	virtual void foo3(){}
    };
    
    int main()
    {
    	A a;
    	int ret = sizeof(A);
    
    	return 0;
    }
    Basically object a contains a virtual table pointer which is of size 4 bytes. Since class A should have a virtual table which contains three pointers pointing to foo, foo2,foo3 separately. So the virtual table should be of size 12 bytes. I wonder where is virtual table located in memory? Thanks.

  2. #2
    Join Date
    Jul 2013
    Posts
    576

    Re: A question regarding the size of a class with virtual functions

    Quote Originally Posted by LarryChen View Post
    I wonder where is virtual table located in memory?
    It's implementation dependent so it's not part of the C++ language and nothing of great concern in everyday programming unless you're writing a C++ compiler or studying compiler technology.

    But since the virtual functions are the same in each object of a class it would be reasonable for efficiency reasons to keep the bulk of the table outside the objects and as little as possible in the objects themselves, ideally just a reference to the table. In that case the overhead will be limited to just one pointer per object regardless of the number of virtual functions in the class.

    http://en.wikipedia.org/wiki/Virtual_method_table
    Last edited by razzle; April 23rd, 2015 at 03:10 AM.

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: A question regarding the size of a class with virtual functions

    The Vtable is NOT necessarily part of the sizeof of a class.

    As razzle said, it's implementation defined.

    For most compilers, a vtable is as it says, a table (array) of virtual methods (function pointers).
    And since each specific instance results in a specific set of virtual overrides, so most compilers tend to "hardcompile" the vtable for each class, and merely set a pointer in the class instance to that table.
    In that case, the sizeof the class will have as many extra bytes as the sizeof a pointer (or replace the 'empty' class dummy).

    But this isn't the only way. There are compilers that have function pointers for each virtual function. so there the sizeof for 3 v-functions would take the size of 3 pointers.

    ANd I even have a compiler where size/memory is a big issue and where instead of a pointer to a vtable, you get a 1byte index to a set of vtables. This is a compiler for an embedded Microprocessor where saving every byte is a big deal.
    And just in case you're wondering, yes, that does mean your entire program is limited to 256 classes with vtables at most
    which isn't an issue at all considering the microprocessor only has 16K, 32K or 64K of eeprom depending on the model.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured