Get C + + virtual function table address and virtual function address

Source: Internet
Author: User

1. First introduce the C + + class memory structure, we can see the following blog, feel good

51995911

Where and this related knowledge point is a virtual function of the first 4 bytes of the class is a pointer to the first address of the virtual function _vfptr

2. The following starts with the specific solution process

Class AA {public:virtual void func1 () {cout << "AA:: Func1" << Endl;}      virtual void Func2 () {cout << "AA:: Func2" << Endl;}  void Func3 () {cout << "aa::func3" << Endl;}    };  typedef void (*fun) (void);      function pointer int main () {AA A; printf ("Virtual table Address:%p\n", * (int *) &a); * * * * * *://1.&a represents the starting address of object A//2. (int *) &a strong to int * type, in order to take the first four bytes of a object, the first four bytes is the virtual table pointer//3.* (int *) &a take the first four bytes, that is vptr the virtual table address//*****printf (" The first virtual function address:%p\n ", * (int *) * (int *) &a); * * * * * * * * * * * (int *) &a is vfptr, that is, virtual table pointer. and virtual table is a virtual function pointer///SO virtual table      Each element (virtual function pointer) is 4 bytes under the 32-bit compiler, so (int *) * (int *) &a//Such a strong turn to fetch four bytes later. so * (int *) * (int *) &a is the first element of a virtual table.      That is, the address of f ().  Then the second virtual function address is followed and so on.      Always remember that vfptr points to a piece of memory,//This memory holds the virtual function address, this memory is what we call the virtual table.      printf ("Virtual table Address:%p\n", * (int *) &a);      printf ("First virtual function Address:%p\n", * (int *) * (int *) &a); printf ("Second virtual function address:%p\n", * (int *) * (inT *) (&AMP;A) + 1);  Fun Pfun = (fun) * ((int *) * (int *) (&a));      Virtual func1 ();      printf ("Func1 ():%p\n", pfun);        Pfun ();  Pfun = (Fun) (* (int *) * (int *) (&a) + 1));      Virtual Func2 ();      printf ("Func2 ():%p\n", pfun);    Pfun ();   }

  

Get C + + virtual function table address and virtual function address

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.