Hacker behavior in C ++ Language

Source: Internet
Author: User

In C ++, we can use the C ++ compiler to implement some tricks for calling virtual functions. We can directly call private member functions of the class outside the class !! The Code is as follows. The code is described in detail.

// Hacker. CPP: hacker behavior in C ++ language <br/> # include <iostream> <br/> class coo {<br/> PRIVATE: <br/> int D; // put D at the beginning, but in fact, in the object memory of the COO class <br/> // There is a pointer variable pointing to the vtable array before D <br/> // The C ++ standard does not specify how the virtual function works. implementation, however, most compilers use vtable, <br/> // and place the pointer to it before the actual data member <br/> virtual void Foo (); // Private, and it is virtual, so there will be vtable! <Br/> Public: <br/> CoO (): D (100) {}< br/>}; <br/> void coo: Foo () {<br/> STD: cout <D <"~~~!!! ~~~ "<STD: Endl; <br/>}< br/> int main () {<br/> coo O; <br/> int * P = (int *) (& O); // P forcibly converts the o address to an integer pointer, in this case, <br/> // P stores the vtable memory start address <br/> int ADDR = * (int *) (* P ); // ADDR refers to the content in P, which is forcibly treated as an integer pointer. <br/> // then, the content pointed to by the pointer is taken out, that is, the function coo :: foo address </P> <p> // If coo: foo is not virtual, no vtable exists before D. The output below will be 100 of D, <br/> // But now * P should output the vtable memory address <br/> STD: cout <"The vtable's address: "<* P <STD: Endl; <br/> // The output ADDR is the address of the COO: Foo function. <Br/> STD: cout <"the COO: Foo's address:" <ADDR <STD: Endl; <br/> // to confirm this, you can define a function pointer pfunc and assign the ADDR (converted to the function pointer) to it, <br/> // then call this function pointer, resulting in the execution of function coo: Foo <br/> typedef void (* pfunc) (COO *); <br/> pfunc = (pfunc) (ADDR); <br/> pfunc (& O); // hack! Directly calling the private member function coo: Foo <br/> return 0; <br/>}

Note: No object is created here (that is, the member function is called directly without the this pointer). It does not matter and it cannot die, as long as member data is not used in the member function, it is the same as a static member function. But here Foo uses member D. What should I do? You can also add a coo * parameter when defining the function pointer (as described above), and pass in the o address to access member D.

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.