[原]A simple example about call stack of C

1167阅读 0评论2011-06-28 fera
分类:C/C++

  1. /**
  2.  * Only available on 32-bit i686 machine
  3.  */

  4. #include <iostream>
  5. using namespace::std;
  6. int injection(int i)
  7. {
  8.   cout << "injection" << " " << i << endl;
  9.   return 0;
  10. }
  11. int main()
  12. {
  13.   int a;
  14.   // Save the proper return address of main().
  15.   unsigned int addr = *(&a + 2);
  16.   // Set return address of main to be first instruction of injection().
  17.   *(&a + 2) = (unsigned int)injection;
  18.   // Set return address of injection() to be that of main() so that
  19.   // main() will return properly.
  20.   *(&a + 3) = addr;
  21.   // Set the argument of injection().
  22.   *(&a + 4) = 99;
  23.   return 0;
  24. }
上一篇:[原]General Idea of Exception Handling in C++
下一篇:[原]Simply Scheme(1):第九章部分习题解答