Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vtable modifications at run time

Tags:

c++

vtable

For those compiler implementations that use vtables: are there any cases when virtual functions tables are changed at run time? Or are vtables only filled at compile time, and no actions are performed to modify them at run time?

like image 761
Karolis Milieška Avatar asked Apr 17 '16 08:04

Karolis Milieška


People also ask

Is vtable created at compile time?

Virtual tables are created during compilation but used at runtime. Show activity on this post. As you say, the virtual function table (and other polymorphic information) for each class is generated at compile time. Each object contains a pointer to the correct table for its dynamic type.

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.

What is vtable and memory allocation in vtable?

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.

What is vtable in C++?

vTable : Every class that contains a virtual function or more has a vTable assigned to it. vTable is a different kind of function pointer array that has the address of all virtual functions of the class. These are built by the compiler during compile time.

What do vtables have to do with early binding?

This process is known as static dispatch or early binding: the compiler knows which routine to execute during compilation. So, what do vtables have to do with all this? Well, there are cases where it is not possible for the compiler to know which routine to execute at compile time. This is the case, for instance, when we declare virtual functions:

What is the difference between vtable and vptr in C++?

5. vtable is created at compile time and is used at run time. 6. vtable will be having function pointer entry to all the virtual functions present and derived in the class. 7. vtable will store NULL value for pure virtual function. 1. vptr is also known as vtable pointer. This pointer will point to vtable.

What are VTABLE and vpointer in Java?

These are built by the compiler during compile time. vPointer : vPointers are associated with every objects of a class containing a vTable. They are stored in the first 4 bytes of memory. vPointers are used to find the function address during the runtime to do the binding as the virtual functions linking are not done at compile time.


Video Answer


2 Answers

I am not aware of any C++ ABI with a polymorphism implementation that employs virtual tables changing at runtime.

It wouldn't be very useful, anyway, since virtual tables typically describe a property of the code (the relationship of member functions to each other w.r.t. position in class hierarchy) and C++ code doesn't change at runtime.

And because it wouldn't be useful, it would be wasteful.

like image 81
Lightness Races in Orbit Avatar answered Nov 01 '22 22:11

Lightness Races in Orbit


The short answer is no.

A slightly longer (and probably implementation specific) answer is that the object's pointer to the actual vtable changes during the execution of a constructor and destructor of a derived polymorphic class, so that overridden methods in a derived class do not get executed by the base class's constructor/destructor while the derived class is not yet constructed/has been destructed.

If you want objects to change class during run time then you have a number of options:

  1. objective-c(++)

  2. hand-code your own dispatch mechanism

  3. python/javascript etc al.

  4. (the best option) reconsider your design.

like image 36
Richard Hodges Avatar answered Nov 02 '22 00:11

Richard Hodges