Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct Test
- {
- Test(int &i) : ref(i){}
- int& ref;
- };
- int main()
- {
- std::cout << "Size of class: " << sizeof(Test) << std::endl;
- int x = 1;
- int y = 2;
- Test t(x); // t.ref refers to x
- std::cout << "Value of ref before hack: " << t.ref << std::endl;
- /* Assuming the reference is actually a pointer on the stack at address &t,
- * write &y to this loication */
- *(int**)(&t) = &y; // t.ref now refers to y
- std::cout << "Value of ref after hack: " << t.ref << std::endl;
- y = 3;
- std::cout << "Value of ref after hack, with y changed: " << t.ref << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement