c ++ función virtual y ejecutable

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

Since all the things you talk about are outside the standard, you must be asking about how a specific implementation works. So maybe you tell us what implementation you talk about. -

Unless you are in a very memory constrained environment, you will not notice the sizes of the vtables (just a few pointers shared by all objects of the same type), it will more probably have a higher impact the extra vote in each instance of the type. -

to PlasmaHH:These questions were asked in a telephonic interview and nothing was mentioned about the specific implementation -

to David Rodriguez: These questions were asked in telephonic interview and the stress was on memory and speed optimization -

4 Respuestas

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.

Respondido 02 Feb 12, 15:02

Thanks Als for the helpful comments. For 2nd point: Can vtable reside outside of exe? - user1184900

@user1184900: ofcourse, not. An executable of your program can run just by itself(with dynamic library dependencies in place) so the vtable must reside in the exe. - Alok Save

This is completey compiler-dependent. The C++ standard has no notion of a vtable.

Respondido 02 Feb 12, 15:02

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.

Respondido 02 Feb 12, 15:02

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.

Respondido 02 Feb 12, 15:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.