SHARE
TWEET

Nick

a guest Sep 12th, 2008 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top