Nonlocal Jump
What is Nonlocal Jump?
Nonlocal jump 允許你從一個函數內直接跳到另一個函數,而不用遵守正常的 calling 和 return 程序
What is it used for?
Escape Deeply Nested Function
假設有一個很深的函數呼叫 main() → f1() → f2() → f3() → f4(),當我們在 f4() 遇到錯誤時,我們想要直接跳回 main() 進行錯誤處理而非一個個 return 回去,這時我們就可以使用 nonlocal jump
Useful for error recovery and signal handling
setjmp, longjmp
sigsetjmp, siglongjmp
D-SP-Ch9bab-sigsetjmp_siglongjmp
Potential Problem of Nonlocal Jump
Works within stack discipline
因為呼叫 P3() 時會覆蓋掉 P2() 的 stack frame,所以當我們從 P3() long jump 到 P2() 時雖然 register 會回復,但 setjmp() 時的 stack frame 已經不見了
