Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

When compiled for i386 using GCC v4.1.2, the above example outputs the following when run:

Code Block
i = 10
longjmp: i = 0

Because g() has finished executing at the time longjmp() is called, it is no longer on the stack. When h() is invoked, its stackframe overwrites the stackframe of g(). In this case i was located in the same location as the end of array b. The call to memset() sets the four bytes that i occupied in g() to 0, so when longjmp() sends control back to function g(), it prints out a value of 0.

...