Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where in memory is vtable stored?

Tags:

c++

vtable

Where in memory is vtable stored?

like image 977
Sandeep Avatar asked Dec 15 '09 04:12

Sandeep


People also ask

What is stored in vtable?

The vtable contains an entry for each virtual function accessible by the class and stores a pointer to its definition. Only the most specific function definition callable by the class is stored in the vtable.

Where are VPTR files stored?

Since, static members are stored in the data section (. data), the vtable is also stored in the data section of the executable. The vptr is initialized to the vtable in the constructor. Regarding Dlls, actually the dlls are mapped inside the address space of the process which is using them.

How is vtable implemented?

To implement virtual functions, C++ uses a special form of late binding known as the virtual table. The virtual table is a lookup table of functions used to resolve function calls in a dynamic/late binding manner.

What is use of vtable in in inheritance?

You can imagine what happens when you perform inheritance and override some of the virtual functions. The compiler creates a new VTABLE for your new class, and it inserts your new function addresses using the base-class function addresses for any virtual functions you don't override.


2 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; }; 
like image 51
Alex Budovski Avatar answered Oct 04 '22 06:10

Alex Budovski


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.

like image 32
jalf Avatar answered Oct 04 '22 07:10

jalf