Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,

I know there are so many existing forums on the same topic. But they are not clear.
so my question is..
When is the virtual table created in c++ whether compile-time or run-time?

In few of the forums it is mentioned that,at compile time only the layout of the table gets created (what does it means) and at run time it gets entries in the table.

Can someone give me brief overview of this?

Thanks in advance
Posted
Comments
Sergey Alexandrovich Kryukov 26-Dec-12 2:39am    
Come to think about, not a bad question, I voted 5 for it.
Cheers,
—SA

This article talks about virtual tables - Polymorphism in C[^]
 
Share this answer
 
Such a simple question, but I knew even pretty experienced developers who have been confused.

The answer depends on what you call a "virtual table". It does exist as the image in the object file, and later it comes to the executable file and finally loaded to the memory. In all stages, it is actually exists as some artifact.

First thing to understand is: there is no more than one virtual table per class. All the instances (objects) of this class share the same instance of the virtual table. If you think about how OOP works with the help of those virtual table, you will understand that this is quite enough to implement the OOP functionality: late binding and hence polymorphism.

Everything else comes with understanding of this fact, and understanding of the code life cycle. Compilation creates an object file, and the virtual table is already there in some form, as well as everything else which makes the class. Then the linker put it all together, as some classes (even of the same class hierarchy) may appear in different object or static library files. In the fully linked form, all classes make their way to the executable file (.EXE, .DLL, .so or anything, PE, elf, or any other executable file format). So, the virtual tables exist already. And then the system loader arranges all the code in memory. This is not really runtime, but something which happens before, I would call it "load time". Then, even if some class pre-elaboration may take place during runtime (this is implementation-dependent), the virtual tables themselves are already there.

—SA
 
Share this answer
 
v2
Comments
Philippe Mori 26-Dec-12 10:11am    
As an addtion, the pointer to the appropriate vtable is updated during the construction (and destruction) of a class. Thus in standard C++ contructors are not virtual and calling a virtual function from a constructor will only call the function at that level.
Sergey Alexandrovich Kryukov 26-Dec-12 12:14pm    
Thank you, Philippe.
Of course, but this is the creation of an instance. Calling of the constructor does not modify the virtual table itself.
In systems with virtual constructors, the situation is somewhat different. The instance is already fully constructed, in terms of virtual mechanism, but if can be modified by the constructor code. That's why calling of virtual methods from a constructor is always safe. In C++, this is a problem and is best avoided.
—SA
The compiler already needs to know which function's address is in cell number n of the v-table of a class. Otherwise it couldn't generate code to call that function. So basically the answer to your question is: V-tables are prepared at compile time. So the compiler for instance knows that function "MyFuncXyz" sits in V-table cell 3.

But: The exact address of a function is sometimes not known at compile time, e.g. when the function resides in another DLL. When the DLL is loaded at runtime the loader may find that it has to be relocated to another base address. In that case, all cells in your program that refer to a function in that DLL will be modified accordingly. And so, also the cells of a V-table get updated in that relocation process.

That is why some people say that V-tables are being prepared "in some form" at compile time, but are being finished at runtime.

I hope that clarifies things for you.
 
Share this answer
 
Comments
Philippe Mori 26-Dec-12 10:14am    
One important thing to know is that vtable pointer are updated during the construction (and destruction) of an object. Thus during construction, a virtual call made from the constructor, will use the vtable for that class and not the vtable from the derived class. This is required for exception safety since C++ does not preinitialize class as C# or C++/CLI do.
nv3 26-Dec-12 12:36pm    
Thank you for this important hint Philippe. Indeed there is not a single V-table for a C++ object, but their are multiple tables involved. The V-table pointer is switch from base class to derived class as the constrution process of the object progresses. This is not a minor detail, but becomes very important when calling virtual functions from a constructor.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900