Where in memory is vtable stored?
6 Answers
Depends on compiler.
In VC++, the vtable pointer stored at the beginning of the object allocation, before any member data. (Provided your class has at least one virtual member function.)
There also may be multiple vtable pointers, if your class multiply-inherits from other classes with vtables.
The vtables themselves are statically allocated somewhere in your address space.
Then the object layout looks like (for an instance of C):
A's VTable ptr
A's member variables.
B's Vtable ptr
B's member variables.
C's member variables.
for the heirarchy
class A {
virtual Ax() {}
int a, b;
};
class B {
virtual Bx() {}
int c, d;
};
class C : public A, public B {
int foo, bar;
};

- 17,947
- 6
- 53
- 58
-
29I think you're talking about vptr's, that is, pointers to vtables. Vtables themselves are generally stored in the static data segment, as they are class-specific (vs. object-specific). – Drew Hall Dec 15 '09 at 05:03
-
1The object layout is for an instance of C, right? (As opposed to an A, a B and a C) – Niklas Dec 15 '09 at 08:58
Vtable? What vtable? The C++ standard doesn't mention a vtable. Each compiler can implement virtual functions any way it likes. And that includes placing the vtable anywhere it likes.

- 243,077
- 51
- 345
- 550
-
I believe that the vtable can be anywhere except at the starting address of the object. My understanding is that the address of the first element is also the starting address of the structure. – Thomas Matthews Dec 15 '09 at 18:29
-
4No, that's not a requirement. It is the case for POD types, but POD types don't have a vtable, so it's a non-issue there. For non-POD types, there's no guarantee that the address of the structure is the same as the address of the first element. – jalf Dec 15 '09 at 18:32
The vptr commonly at the beginning of the object (Imperfect C++, Backyard Hotrodding C++) but that's not guaranteed in the standard. Using vptrs and vtables isn't guaranteed in the standard.
If you really need to know where it is, it's common to use something like COM, XPCOM, UNO, etc. that are implemented by essentially coming up with a set place where something like a vptr is located and set ways to use them.

- 19,717
- 4
- 46
- 69
-
CM and CORBA C++ bindings that I've used leave the placement of the vtable up to the C++ compiler. – Dec 15 '09 at 12:53
-
Every instance that include virtual function has virtual function pointer which point to the virtual function table(vbtl), we only could locate the vtbl through the instance. or you can use the objdump to read the symbol of the ELF file, maybe you can find the answer. I hope The following example can hep you.
#include <iostream>
#include <stdio.h>
typedef void (*fun_pointer)(void);
using namespace std;
class Test
{
public:
Test()
{
cout<<"Test()."<<endl;
}
virtual void print()
{
cout<<"Test::Virtual void print()."<<endl;
}
virtual void print2()
{
cout<<"Test::virtual void print2()."<<endl;
}
};
class TestDrived:public Test
{
public:
TestDrived()
{
cout<<"TestDrived()."<<endl;
}
virtual void print()
{
cout<<"TestDrived::virtual void print()."<<endl;
}
virtual void print2()
{
cout<<"TestDrived::virtual void print2()."<<endl;
}
void GetVtblAddress()
{
cout<<"vtbl address:"<<(int*)this<<endl;
}
void GetFirstVtblFunctionAddress()
{
cout<<"First vbtl function address:"<<(int*)*(int*)this+0 << endl;
}
void GetSecondVtblFunctionAddress()
{
cout<<"Second vbtl function address:"<<(int*)*(int*)this+1 << endl;
}
void CallFirstVtblFunction()
{
fun = (fun_pointer)* ( (int*) *(int*)this+0 );
cout<<"CallFirstVbtlFunction:"<<endl;
fun();
}
void CallSecondVtblFunction()
{
fun = (fun_pointer)* ( (int*) *(int*)this+1 );
cout<<"CallSecondVbtlFunction:"<<endl;
fun();
}
private:
fun_pointer fun;
};
int main()
{
cout<<"sizeof(int):"<<sizeof(int)<<"sizeof(int*)"<<sizeof(int*)<<endl;
fun_pointer fun = NULL;
TestDrived a;
a.GetVtblAddress();
a.GetFirstVtblFunctionAddress();
a.GetSecondVtblFunctionAddress();
a.CallFirstVtblFunction();
a.CallSecondVtblFunction();
return 0;
}

- 34,448
- 50
- 182
- 322

- 45
- 1
- 4
Vptr and Vtable get stored in Data Segment...
Vtable is like an array of function pointer.
Vtable and Vptr is creating at compile time which will get memory in run time and vtable entries are virtual function addresses .
Every object of a class containing a virtual function will have an extra pointer which is pointing to Virtual Table is known as virtual pointer.
whenever we call a virtual function using object, first the corrosponding Vptr will read the function from Vtable at run time and finally the function will be called.