Thinking about the storage location of virtual function table in C + +

Source: Internet
Author: User

In fact, this is a question I thought about a while ago, is in the "deep exploration of the C + + object model" This book I produced a question, recently on the internet and see similar posts, posted to see:



I see a lot of interesting answers, all of them are better, and the following are some representative:

Answer 1:


Answer 2:


As we all know, virtual function is the basis of a polymorphic mechanism, that is, in the runtime according to the invocation of the object to determine the specific call which function, now we say its concrete implementation principle, mainly say my own understanding, if there is something wrong, please correct me
At the front of each object that contains a virtual Function's memory layout, which is the first of its kind in terms of the top, long story, and not to mention the efficiency problem, there is something called virtual function pointer (vptr) pointing to the virtual function table (vtbl), the virtual function table ( This is only the simplest case of single inheritance, if multiple inheritance, there may be more than one virtual function table inside the class contains all the virtual functions of the pointer, when we want to invoke the inside of the function by looking for this virtual function table to find the corresponding virtual function, this is the implementation of virtual function Principle. Here I assume everyone knows, if not understand can go to check the Information. well, since we know the virtual function of the implementation of the principle, virtual function pointer vptr point to the virtual function table vtbl, and vptr again at the front of the object, then we can easily get the address of the virtual function table, below I wrote a piece of code tested a bit:

#include <iostream>#include<stdio.h>typedefvoid(*fun_pointer) (void);using namespacestd;classtest{ public: Test () {cout<<"Test ()."<<endl; }   Virtual voidPrint () {cout<<"test::virtual void Print1 ()."<<endl; }   Virtual voidPrint2 () {cout<<"test::virtual void Print2 ()."<<endl; }};classTestdrived: publictest{ public: Static int var; testdrived () {cout<<"testdrived ()."<<endl; }  Virtual voidPrint () {cout<<"testdrived::virtual void Print1 ()."<<endl; }  Virtual voidPrint2 () {cout<<"testdrived::virtual void Print2 ()."<<endl; }  voidgetvtbladdress () {cout<<"VTBL address:"<< (int*) this<<endl; }  voidgetfirstvtblfunctionaddress () {cout<<"first vbtl funtion address:"<< (int*)*(int*) this+0<<endl; }  voidgetsecondvtblfunctionaddress () {cout<<"Second vbtl funtion address:"<< (int*)*(int*) this+1<<endl; }  voidcallfirstvtblfunction () { fun= (fun_pointer) * ((int*) *(int*) this+0 ); cout<<"callfirstvbtlfunction:"<<endl;    Fun (); }  voidcallsecondvtblfunction () { fun= (fun_pointer) * ((int*) *(int*) this+1 ); cout<<"callsecondvbtlfunction:"<<endl;    Fun (); }Private: Fun_pointer fun;};inttestdrived::var=3;intmain () {cout<<"sizeof (int):"<<sizeof(int) <<"sizeof (int*)"<<sizeof(int*) <<endl; Fun_pointer fun=NULL; Testdrived a; A.getvtbladdress (); cout<<"the var ' s address is:"<<&testdrived::var<<endl; a.getfirstvtblfunctionaddress (); a.getsecondvtblfunctionaddress (); a.callfirstvtblfunction (); A. Callsecondvtblfunction (); return 0;}

Here we call the virtual function inside by getting the address of the virtual function table.

These days to check the information, finally figuring out the virtual function table vtable in the Linux/unix in the executable read-only data section (rodata), and Microsoft's compiler to store virtual function table in the constant segment there are some Differences. Compile the above file to generate the final executable file, and then use the Command:
objdump-s-x-d a.out | c++filt | grep "vtable" can get the following output

It is clear that these two classes of test and testdrived are stored in The. rodata, as for the above command, a little explanation of the following, objdump can read
Details in the executable file, including the header of the executable file, section, symbol, and so on, with Objdump to obtain the symbols of the executable file many are
We do not understand, or the same as the function or variable in our source code, this is because C + + supports function overloading, C + + for all symbols are done
modification, A lot of information is called "function signature" or "symbolic decoration" similar concepts, but we want to convert it into our source code of the symbol, which will be used to
C++filt command, well, to come here to the conclusion, in short, about the virtual function table detailed details are introduced Here.

A few noteworthy questions

    1. The virtual function table is class specific, that is, for a class, it is a bit like a STAIC member variable inside a class, that is, it belongs to all objects of a class, not to an object-specific, and is common to all objects of a class.
    2. The virtual function table is the compiler to choose the implementation of the compiler, the kind of different, may be implemented differently, as we said earlier vptr at the front of an object, but there are other implementations, but the current GCC and Microsoft compiler is to put vptr in the object memory layout of the Front.
    3. Although we know that Vptr point to the virtual function table, then the virtual function table is stored in the memory where it is, although here we can get the address of the virtual function table. A virtual function pointer is actually initialized when the constructor executes, and the virtual function table is stored in the executable file. The following blog post tests the Microsoft compiler to store virtual function tables in a constant section of the target file or executable file, http://blog.csdn.net/vicness/article/details/ 3962767, but I did not find the specific location of the VTBL in the assembly file under gcc, mainly on the loading and operation of the executable is not a deep understanding of the principle, I believe that soon after this knowledge will be very easy to find the virtual function table is stored in the target file in which Segment.
    4. After testing, the virtual function table vtable in the implementation of the GCC compiler is stored in the Read-only data segment of the executable File. Rodata.
Reference Documents:
1. http://blog.csdn.net/vicness/article/details/3962767
2. Deep Exploration of the C + + object model

Thinking about the storage location of virtual function table in C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.