Virtual Pointer Size-C ++

What is the size of a virtual pointer (VPTR) for a virtual table in C ++? Nor is it a homework question ... just a question that came to my mind when I read a book in C ++.

+4
source share
6 answers

A great article related to this topic is Member Function Pointers and C ++ Fastest Delegates . This article delves deeply into the implementation of member function pointers for many different compilers. This article talks about all the nuances of vtable pointers, especially in light of multiple (and virtual) inheritance.

+6
source

Note that in order to gracefully handle multiple inheritance, an object can have more than one VPTR, but in general, each of them is likely to be a simple architecture-specific pointer.

Try running something like this to see how your compiler puts things out:

#include <iostream> using namespace std; struct Base { int B; virtual ~Base() {} }; struct Base2 { int B2; virtual ~Base2() {} }; struct Derived : public Base, public Base2 { int D; }; int main(int argc, char* argv[]) { cout << "Base:" << sizeof (Base) << endl; cout << "Base2:" << sizeof (Base2) << endl; cout << "Derived:" << sizeof (Derived) << endl; Derived *d = new Derived(); cout << d << endl; cout << static_cast<Base*>(d) << endl; cout << &(d->B) << endl; cout << static_cast<Base2*>(d) << endl; cout << &(d->B2) << endl; cout << &(d->D) << endl; delete d; return 0; } 

In my 32-bit compiler, this gives 8 bytes for the base classes and 20 bytes for the Derived class (and doubles these values ​​when compiling for 64 bits):

 4 bytes Derived/Base VPTR 4 bytes int B 4 bytes Derived/Base2 VPTR 4 bytes int B2 4 bytes int D 

You can see how, looking at the first 8 bytes, you can treat Derived as a base, and how, looking at the second 8 bytes, you can treat it like Base2.

+3
source

It depends on your implementation, but it is easy to find out. For this program

 #include <iostream> struct virtual_base { int data; virtual_base() {} virtual ~virtual_base() {} }; struct non_virtual_base { int data; non_virtual_base() {} ~non_virtual_base() {} }; int main() { std::cout << sizeof( virtual_base ) - sizeof( non_virtual_base ) << '\n'; return 0; } 

mine (VC 2008) will print 4, so the cost of polymorphism in this case is 4 bytes.

+2
source

Probably the same size as a regular pointer ... usually 4 bytes on 32-bit machines. But this will be compiler dependent, some compilers may do something differently.

+1
source

Most likely the size of any other pointer. Try something like this to find out your compiler and machine:

 #include <iostream> struct base { base() {} virtual ~base() {} }; int main( int argc, char **argv ) { std::cout << sizeof( base ) << std::endl; } 
+1
source

Pointers in a virtual function table are usually the same size as regular pointers on a system. Typically, a table of virtual functions is computed for each type, and each instance of the object will contain a pointer to its type table, so instances of objects containing virtual functions will use sizeof(void *) bytes more for each instance than those that do not. Types based on several basic types must be bound to any basic type; therefore, if necessary, they can contain several pointers to tables of virtual functions of basic types. All this, of course, depends on the compiler.

+1
source

All Articles