Advertisement
Guest User

Nick

a guest
Sep 12th, 2008
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1.  
  2. #include <iostream>
  3.  
  4. struct Test
  5. {
  6.     Test(int &i) : ref(i){}
  7.     int& ref;
  8. };
  9.  
  10. int main()
  11. {
  12.     std::cout << "Size of class: " << sizeof(Test) << std::endl;
  13.     int x = 1;
  14.     int y = 2;
  15.     Test t(x); // t.ref refers to x
  16.  
  17.     std::cout << "Value of ref before hack: " << t.ref << std::endl;
  18.     /* Assuming the reference is actually a pointer on the stack at address &t,
  19.      * write &y to this loication */
  20.     *(int**)(&t) = &y; // t.ref now refers to y
  21.     std::cout << "Value of ref after hack: " << t.ref << std::endl;
  22.     y = 3;
  23.     std::cout << "Value of ref after hack, with y changed: " << t.ref << std::endl;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement