我必须在 'setjmp' 之后调用 'longjmp' 吗?
Do I have to call 'longjmp' after a 'setjmp'
在 longjmp
和 setjmp
的联机帮助页中,有这一行:
If the function which called setjmp()
returns before longjmp()
is
called, the behaviour is undefined.
这是否意味着我实际上必须在调用 setjmp
的函数或嵌套函数中的某处调用 longjmp
?或者根本不调用它可以吗?
你读错了。
If the function which called setjmp() returns before longjmp() is called, the behaviour is undefined.
如果在 从设置 setjmp
的函数返回后 调用它,longjmp
的行为是未定义的。但是完全可以不调用longjmp
。
Wikipedia更清楚:
If the function in which setjmp was called returns, it is no longer possible to safely use longjmp with the corresponding jmp_buf object.
This is because the stack frame is invalidated when the function returns. Calling longjmp restores the stack pointer, which—because the function returned—would point to a non-existent and potentially overwritten or corrupted stack frame.
这些函数通常用于处理异常机制。如果没有发生异常,您不想调用 longjmp
因为没有理由“倒回”您的程序。
在 longjmp
和 setjmp
的联机帮助页中,有这一行:
If the function which called
setjmp()
returns beforelongjmp()
is called, the behaviour is undefined.
这是否意味着我实际上必须在调用 setjmp
的函数或嵌套函数中的某处调用 longjmp
?或者根本不调用它可以吗?
你读错了。
If the function which called setjmp() returns before longjmp() is called, the behaviour is undefined.
如果在 从设置 setjmp
的函数返回后 调用它,longjmp
的行为是未定义的。但是完全可以不调用longjmp
。
Wikipedia更清楚:
If the function in which setjmp was called returns, it is no longer possible to safely use longjmp with the corresponding jmp_buf object.
This is because the stack frame is invalidated when the function returns. Calling longjmp restores the stack pointer, which—because the function returned—would point to a non-existent and potentially overwritten or corrupted stack frame.
这些函数通常用于处理异常机制。如果没有发生异常,您不想调用 longjmp
因为没有理由“倒回”您的程序。