Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Virtual method table
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Mechanism for supporting dynamic dispatch}} {{redirect|Virtual table|virtual tables in SQL|View (SQL)}} {{Use dmy dates|date=January 2021}} In [[computer programming]], a '''virtual method table''' ('''VMT'''), '''virtual function table''', '''virtual call table''', [[dispatch table]], '''vtable''', or '''vftable''' is a mechanism used in a [[programming language]] to support [[dynamic dispatch]] (or [[Run time (program lifecycle phase)|run-time]] method [[name binding|binding]]). Whenever a [[Class (computer programming)|class]] defines a [[virtual function]] (or [[Method (computer programming)|method]]), most [[compiler]]s add a hidden [[member variable]] to the class that points to an array of [[Pointer (computer programming)|pointer]]s to (virtual) functions called the virtual method table. These pointers are used at runtime to invoke the appropriate function implementations, because at compile time it may not yet be known if the base function is to be called or a derived one implemented by a class that [[Inheritance (object-oriented programming)|inherits]] from the base class. There are many different ways to implement such dynamic dispatch, but use of virtual method tables is especially common among [[C++]] and related languages (such as [[D (programming language)|D]] and [[C Sharp (programming language)|C#]]). Languages that separate the programmatic interface of objects from the implementation, like [[Visual Basic]] and [[Delphi (programming language)|Delphi]], also tend to use this approach, because it allows objects to use a different implementation simply by using a different set of method pointers. The method allows creation of external libraries, where other techniques perhaps may not.<ref name=inria-00565627>{{ cite book | url=https://hal.inria.fr/inria-00565627/document |title=Efficient Dynamic Dispatch without Virtual Function Tables: The SmallEiffel Compiler -- 12th Annual ACM SIGPLAN Conference on Object-Oriented Programming Systems, Languages and Applications (OOPSLA'97), ACM SIGPLAN, Oct 1997, Atlanta, United States. pp.125-141. inria-00565627 |page=16 |first1=Olivier |last1=Zendra |first2=Dominique |last2=Colnet |first3=Suzanne |last3=Collin |date=1997 |publisher=Centre de Recherche en Informatique de Nancy Campus Scientifique, Bâtiment LORIA }}</ref> Suppose a program contains three classes in an inheritance hierarchy: a [[Superclass (computer science)|superclass]], {{mono|Cat}}, and two [[Subclass (computer science)|subclasses]], {{mono|HouseCat}} and {{mono|Lion}}. Class {{mono|Cat}} defines a [[virtual function]] named {{mono|speak}}, so its subclasses may provide an appropriate implementation (e.g. either {{mono|meow}} or {{mono|roar}}). When the program calls the {{mono|speak}} function on a {{mono|Cat}} reference (which can refer to an instance of {{mono|Cat}}, or an instance of {{mono|HouseCat}} or {{mono|Lion}}), the code must be able to determine which implementation of the function the call should be ''dispatched'' to. This depends on the actual class of the object, not the class of the reference to it ({{mono|Cat}}). The class cannot generally be determined ''statically'' (that is, at [[compile time]]), so neither can the compiler decide which function to call at that time. The call must be dispatched to the right function ''dynamically'' (that is, at [[run time (program lifecycle phase)|run time]]) instead. ==Implementation== <!-- [[Virtual function pointer]] redirects here --> An object's virtual method table will contain the [[Memory address|addresses]] of the object's dynamically bound methods. Method calls are performed by fetching the method's address from the object's virtual method table. The virtual method table is the same for all objects belonging to the same class, and is therefore typically shared between them. Objects belonging to type-compatible classes (for example siblings in an inheritance hierarchy) will have virtual method tables with the same layout: the address of a given method will appear at the same offset for all type-compatible classes. Thus, fetching the method's address from a given offset into a virtual method table will get the method corresponding to the object's actual class.<ref>Ellis & Stroustrup 1990, pp. 227–232</ref> The [[C++]] standards do not mandate exactly how dynamic dispatch must be implemented, but compilers generally use minor variations on the same basic model. Typically, the compiler creates a separate virtual method table for each class. When an object is created, a pointer to this table, called the '''virtual table pointer''', '''vpointer''' or '''VPTR''', is added as a hidden member of this object. As such, the compiler must also generate "hidden" code in the [[Constructor (object-oriented programming)|constructor]]s of each class to initialize a new object's virtual table pointer to the address of its class's virtual method table. Many compilers place the virtual table pointer as the last member of the object; other compilers place it as the first; portable source code works either way.<ref> Danny Kalev. [http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=196 "C++ Reference Guide: The Object Model II"]. 2003. Heading "Inheritance and Polymorphism" and "Multiple Inheritance". </ref> For example, [[g++]] previously placed the pointer at the end of the object.<ref>{{cite web |url=http://www.codesourcery.com/public/cxx-abi/cxx-closed.html |title=C++ ABI Closed Issues |access-date=2011-06-17 |url-status=bot: unknown |archive-url=https://web.archive.org/web/20110725153606/http://www.codesourcery.com/public/cxx-abi/cxx-closed.html |archive-date=25 July 2011}}</ref> ==Example== Consider the following class declarations in [[C++ syntax]]: <syntaxhighlight lang="cpp"> class B1 { public: virtual ~B1() {} void fnonvirtual() {} virtual void f1() {} int int_in_b1; }; class B2 { public: virtual ~B2() {} virtual void f2() {} int int_in_b2; }; </syntaxhighlight> used to derive the following class: <syntaxhighlight lang="cpp"> class D : public B1, public B2 { public: void d() {} void f2() override {} int int_in_d; }; </syntaxhighlight> and the following piece of C++ code: <syntaxhighlight lang="cpp"> B2 *b2 = new B2(); D *d = new D(); </syntaxhighlight> g++ 3.4.6 from [[GNU Compiler Collection|GCC]] produces the following 32-bit memory layout for the object <code>b2</code>:<ref group="nb">G++'s <code>-fdump-class-hierarchy</code> (starting with version 8: <code>-fdump-lang-class</code>) argument can be used to dump virtual method tables for manual inspection. For AIX VisualAge XlC compiler, use <code>-qdump_class_hierarchy</code> to dump class hierarchy and virtual function table layout.</ref> <pre> b2: +0: pointer to virtual method table of B2 +4: value of int_in_b2 virtual method table of B2: +0: B2::f2() </pre> and the following memory layout for the object <code>d</code>: <pre> d: +0: pointer to virtual method table of D (for B1) +4: value of int_in_b1 +8: pointer to virtual method table of D (for B2) +12: value of int_in_b2 +16: value of int_in_d Total size: 20 Bytes. virtual method table of D (for B1): +0: B1::f1() // B1::f1() is not overridden virtual method table of D (for B2): +0: D::f2() // B2::f2() is overridden by D::f2() // The location of B2::f2 is not in the virtual method table for D </pre> Note that those functions not carrying the keyword <code>virtual</code> in their declaration (such as <code>fnonvirtual()</code> and <code>d()</code>) do not generally appear in the virtual method table. There are exceptions for special cases as posed by the [[default constructor]]. Also note the [[Virtual function#Virtual destructors|virtual destructors]] in the base classes, <code>B1</code> and <code>B2</code>. They are necessary to ensure <code>delete d</code> can free up memory not just for <code>D</code>, but also for <code>B1</code> and <code>B2</code>, if <code>d</code> is a pointer or reference to the types <code>B1</code> or <code>B2</code>. They were excluded from the memory layouts to keep the example simple. <ref group="nb">{{Cite web|url=https://stackoverflow.com/questions/17960917/why-there-are-two-virtual-destructor-in-the-virtual-table-and-where-is-address-o|title = C++ - why there are two virtual destructor in the virtual table and where is address of the non-virtual function (gcc4.6.3)}}</ref> [[Method overriding|Overriding of the method]] <code>f2()</code> in class <code>D</code> is implemented by duplicating the virtual method table of <code>B2</code> and replacing the pointer to <code>B2::f2()</code> with a pointer to <code>D::f2()</code>. ==Multiple inheritance and thunks== The g++ compiler implements the [[multiple inheritance]] of the classes <code>B1</code> and <code>B2</code> in class <code>D</code> using two virtual method tables, one for each base class. (There are other ways to implement multiple inheritance, but this is the most common.) This leads to the necessity for "pointer fixups", also called [[Thunk (programming)|thunk]]s, when [[Type conversion|casting]]. Consider the following C++ code: <syntaxhighlight lang="cpp"> D *d = new D(); B1 *b1 = d; B2 *b2 = d; </syntaxhighlight> While <code>d</code> and <code>b1</code> will point to the same memory location after execution of this code, <code>b2</code> will point to the location <code>d+8</code> (eight bytes beyond the memory location of <code>d</code>). Thus, <code>b2</code> points to the region within <code>d</code> that "looks like" an instance of <code>B2</code>, i.e., has the same memory layout as an instance of <code>B2</code>.{{clarify |reason=because it is not apparent why this should be so, and I am not even sure that the description is accurate if it is truly that way |date=August 2023}} ==Invocation== A call to <code>d->f1()</code> is handled by dereferencing <code>d</code>'s <code>D::B1</code> vpointer, looking up the <code>f1</code> entry in the virtual method table, and then dereferencing that pointer to call the code. '''Single inheritance''' In the case of single inheritance (or in a language with only single inheritance), if the vpointer is always the first element in <code>d</code> (as it is with many compilers), this reduces to the following pseudo-C++: <syntaxhighlight lang="cpp"> (*((*d)[0]))(d) </syntaxhighlight> Where <code>*d</code> refers to the virtual method table of <code>D</code> and <code>[0]</code> refers to the first method in the virtual method table. The parameter <code>d</code> becomes the [[This (computer science)|"<code>this</code>" pointer]] to the object. '''Multiple inheritance''' In the more general case, calling <code>B1::f1()</code> or <code>D::f2()</code> is more complicated: <syntaxhighlight lang="cpp"> (*(*(d[0]/*pointer to virtual method table of D (for B1)*/)[0]))(d) /* Call d->f1() */ (*(*(d[8]/*pointer to virtual method table of D (for B2)*/)[0]))(d+8) /* Call d->f2() */ </syntaxhighlight> The call to <code>d->f1()</code> passes a <code>B1</code> pointer as a parameter. The call to <code>d->f2()</code> passes a <code>B2</code> pointer as a parameter. This second call requires a fixup to produce the correct pointer. The location of <code>B2::f2</code> is not in the virtual method table for <code>D</code>. By comparison, a call to <code>d->fnonvirtual()</code> is much simpler: <syntaxhighlight lang="cpp"> (*B1::fnonvirtual)(d) </syntaxhighlight> ==Efficiency== A virtual call requires at least an extra indexed dereference and sometimes a "fixup" addition, compared to a non-virtual call, which is simply a jump to a compiled-in pointer. Therefore, calling virtual functions is inherently slower than calling non-virtual functions. An experiment done in 1996 indicates that approximately 6–13% of execution time is spent simply dispatching to the correct function, though the overhead can be as high as 50%.<ref>Driesen, Karel and Hölzle, Urs, [https://dl.acm.org/doi/10.1145/236338.236369 "The Direct Cost of Virtual Function Calls in C++"], OOPSLA 1996</ref> The cost of virtual functions may not be so high on modern {{abbr|CPU|Central Processing Unit}} architectures due to much larger caches and better [[branch predictor|branch prediction]]. Furthermore, in environments where [[Just-in-time compilation|JIT compilation]] is not in use, virtual function calls usually cannot be [[inline expansion|inlined]]. In certain cases it may be possible for the compiler to perform a process known as '''devirtualization''' in which, for instance, the lookup and indirect call are replaced with a conditional execution of each inlined body, but such optimizations are not common. To avoid this overhead, compilers usually avoid using virtual method tables whenever the call can be resolved at [[compile time]]. Thus, the call to <code>f1</code> above may not require a table lookup because the compiler may be able to tell that <code>d</code> can only hold a <code>D</code> at this point, and <code>D</code> does not override <code>f1</code>. Or the compiler (or optimizer) may be able to detect that there are no subclasses of <code>B1</code> anywhere in the program that override <code>f1</code>. The call to <code>B1::f1</code> or <code>B2::f2</code> will probably not require a table lookup because the implementation is specified explicitly (although it does still require the 'this'-pointer fixup). ==Comparison with alternatives== The virtual method table is generally a good performance trade-off to achieve dynamic dispatch, but there are alternatives, such as [[binary tree dispatch]], with higher performance in some typical cases, but different trade-offs.<ref name=inria-00565627 /><ref>Zendra, Olivier and Driesen, Karel, [http://www.usenix.org/event/jvm02/full_papers/zendra/zendra_html/index.html "Stress-testing Control Structures for Dynamic Dispatch in Java"], pp. 105–118, Proceedings of the USENIX 2nd Java Virtual Machine Research and Technology Symposium, 2002 (JVM '02)</ref> However, virtual method tables only allow for [[single dispatch]] on the special "this" parameter, in contrast to [[multiple dispatch]] (as in [[Common Lisp Object System|CLOS]], [[Dylan (programming language)|Dylan]], or [[Julia (programming language)|Julia]]), where the types of all parameters can be taken into account in dispatching. Virtual method tables also only work if dispatching is constrained to a known set of methods, so they can be placed in a simple array built at compile time, in contrast to [[duck typing]] languages (such as [[Smalltalk]], [[Python (programming language)|Python]] or [[JavaScript]]). Languages that provide either or both of these features often dispatch by looking up a string in a [[hash table]], or some other equivalent method. There are a variety of techniques to make this faster (e.g., [[String interning|interning]]/tokenizing method names, caching lookups, [[just-in-time compilation]]). ==See also== *[[Virtual function]] *[[Virtual inheritance]] *[[Branch table]] ==Notes== <references group=nb/> ==References== * Margaret A. Ellis and [[Bjarne Stroustrup]] (1990) The Annotated C++ Reference Manual. Reading, MA: Addison-Wesley. ({{ISBN|0-201-51459-1}}) {{Reflist}} {{Application binary interface}} {{DEFAULTSORT:Virtual Method Table}} [[Category:Method (computer programming)]] [[Category:Articles with example C++ code]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Abbr
(
edit
)
Template:Application binary interface
(
edit
)
Template:Cite book
(
edit
)
Template:Cite web
(
edit
)
Template:Clarify
(
edit
)
Template:ISBN
(
edit
)
Template:Mono
(
edit
)
Template:Redirect
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Use dmy dates
(
edit
)