There are a few questions concerning vptr and exe:
1) When is vTable created and populated ie compile-time or run-time?
2) Does compiler put vptr in exe ie size of exe increases with no. of classes having virtual functions
3) Does executable size grows when we run it and when it contain virtual functions
preguntado el 02 de febrero de 12 a las 11:02
This is all implementation defined and will vary from compiler to compiler, The standard does not mention how dynamic dispatch should be implemented, it does not even use the words vtable and vpointer, but all known compilers implement dynamic dispatch through vtable and assuming that answers to your questions are:
When is vTable created and populated ie compile-time or run-time?
Tiempo de compilación
vtable is created for each class with atleast one virtual method during compilation phase.
Does compiler put vptr in exe ie size of exe increases with no. of classes having virtual functions?
Si, lo mas probable
Since the vtable has to reside somewhere in memory, it will occupy some memory space for sure.
Does executable size grows when we run it and when it contain virtual functions?
No there is no runtime growth of exe.
Only the dispatch of functions happens at run-time, the mechanism to make that dispatch happen is constructed at compilation.
This is completey compiler-dependent. The C++ standard has no notion of a vtable.
The actual implementation isn't defined by the standard, but typically, there will usually be only one vtable per class, with static lifetime. This will increase the size of the executable, but normally only marginally—nothing significant.
The vptr is part of the class image: it will increase the size of a class instance by the size of a pointer (or more, depending on alignment restrictions and where the compiler puts it in the class). It will also cause additional code to be generated for the constructor; again, this is almost always neglible. The fact that often a virtual function cannot be inlined can have a significant impact on performance, and in some cases, size as well; inlining a simple function can often expose additional possibilities for optimization.
Usually, for gcc, the VTable is created at compile-time, although it may be adjusted at runtime if you have
-fPIC. It costs a few bytes per class, per function: a negligible amount compared to everything else.