20130330

How to see vtable virtual table for virtual functions in cpp


Virtual table is look up table for virtual functions. It contains the function pointer to the virtual functions. Virtual table is created for each class having one or more virtual function and classes derived from that class.virtual pointer better known as vptr containing the address of vtable is created for the most base class thats why size of empty class with virtual function is equal to the size of a pointer(8 bytes for a 64 bit computer)while size of a empty class without any virtual functions is only one byte.
Command to see vtable/virtual table of a class.
Here if the name of the program is "classWithVf.cxx" so the command to see the vtable of the class is:

g++ -fdump-class-hierarchy classWithVf.cxx


#include<iostream>
using namespace std;

class M //empty class
{
public:
void f() {}
};
class A //empty class
{

public:
void f() {}
};

class B//empty class with 1 vf
{
public:
virtual void f() {}
};
class B1//class with 1 vf 1 dm
{
private:
char x1;
public:
virtual void f1() {}
};
class D : public B1
{
private:
char z;
public:
void f1() {}
};
class B2//clas without vf 1dm
{
private:
char a;
public:
void f2() {}
};

class B3
{
private:
char b31;
public:
void f3() {}
};



int main ()
{
M m;
cout<<"size of m:"<<sizeof(m)<<endl;// 1 obj with def constr
cout<<"size of M:"<<sizeof(M)<<endl;// 1

A *a=new A;
cout<<"size of a:"<<sizeof(a)<<endl;// 8 object with new so a is ptr here
cout<<"size of A:"<<sizeof(A)<<endl;// 1

B b;
cout<<"size of b:"<<sizeof(b)<<endl;// 8 empty class with vf
cout<<"size of B:"<<sizeof(B)<<endl;// 8

B1 b1;
// B1 *b1 = new B1();
cout<<"size of b1:"<<sizeof(b1)<<endl;//16 if obj by new //else if by def constr 8
cout<<"size of B1:"<<sizeof(B1)<<endl;//16 bytes
D *d = new D();
cout<<"size of d1:"<<sizeof(d)<<endl;//8
cout<<"size of D1:"<<sizeof(D)<<endl;//16
B2 b2;
cout<<"size of b2:"<<sizeof(b2)<<endl;//1//obj by def constr
cout<<"size of B2:"<<sizeof(B2)<<endl;//1
B3 *b3 = new B3;
cout<<"size of b3:"<<sizeof(b3)<<endl;//8 obj by new
cout<<"size of B3:"<<sizeof(B3)<<endl;//1
return 0;
}

/*
* g++ -fdump-class-hierarchy classWithVf.cxx
gvim classWithVf.cxx.t01.class

Class M
size=1 align=1
base size=0 base align=1
M (0x2b9ff89feb60) 0 empty

Class A
size=1 align=1
base size=0 base align=1
A (0x2b9ff8a19070) 0 empty

Vtable for B
B::_ZTV1B: 3u entries
0 (int (*)(...))0
8 (int (*)(...))(& _ZTI1B)
16 B::f

Class B
size=8 align=8
base size=8 base align=8
B (0x2b9ff8a19150) 0 nearly-empty
vptr=((& B::_ZTV1B) + 16u)

Vtable for B1
B1::_ZTV2B1: 3u entries
0 (int (*)(...))0
8 (int (*)(...))(& _ZTI2B1)
16 B1::f1

Class B1
size=16 align=8
base size=9 base align=8
B1 (0x2b9ff8a19460) 0
vptr=((& B1::_ZTV2B1) + 16u)

Vtable for D
D::_ZTV1D: 3u entries
0 (int (*)(...))0
8 (int (*)(...))(& _ZTI1D)
16 D::f1

Class D
size=16 align=8
base size=10 base align=8
D (0x2b9ff8a19770) 0
vptr=((& D::_ZTV1D) + 16u)
B1 (0x2b9ff8a197e0) 0
primary-for D (0x2b9ff8a19770)

Class B2
size=1 align=1
base size=1 base align=1
B2 (0x2b9ff8a19a80) 0

Class B3
size=1 align=1
base size=1 base align=1
B3 (0x2b9ff8a19b60) 0

*/

Note:
Sizeof returns +1 for \0
Strlen returns exact
Previous                             Home                               Next

1 comment:

  1. What is the command of to see virtual table in cpp?
    command to see structure of virtual table in c++?

    if your C++ program is classWithVf.cxx then it would be:

    g++ -fdump-class-hierarchy classWithVf.cxx

    ReplyDelete