CS360 Final -- December 10, 2002
Answers to Question 4
- First it calls setjmp(j), which returns zero. The first line is ``Main: 0''.
- Next, it calls a(0, j). This calls setjmp(m), which again returns zero.
Thus, the second line is ``A: 0''.
- Now, a() calls b(0, j, m), which prints ``B: 0'', and calls longjmp(j, 1).
- The longjmp() will return from the setjmp() call in main(), which
now sets i to 1. The next line will be ``Main: 1''.
- Once again, main() calls a(), but this time with the arguments 1 and j.
a() calls setjmp() again, which returns zero, so i is still 1. Thus,
the next line is ``A: 1.''
- Now, a() calls b(1, j, m), which prints ``B: 1'', and calls longjmp(m, 2).
- The longjmp() returns 2 from the setjmp() call in a(), which means that
i is now set to 3, and a() prints out ``A: 3.''
- Now, a() calls b(3, j, m), which prints ``B: 3'', and calls longjmp(m, 4).
- The longjmp() returns 4 from the setjmp() call in a(), which means that
i is now set to 7, and a() prints out ``A: 7.''
- Now, a() calls longjmp(j, 7), which returns 7 from the setjmp() call in
main(). Thus, main() prints ``Main: 7'' and the program exits.
Thus, the output is:
Main: 0
A: 0
B: 0
Main: 1
A: 1
B: 1
A: 3
B: 3
A: 7
Main: 7
Grading: 5 points
- 1 point for the first three lines
- 1 point for lines 4-6
- 1 point for 7-8
- 1 point for line 9 (half credit if you did not get 7)
- 1 point for line 10 (half credit if you did not get 7)