I was investigating setjmp/longjmp and found out that setjmp saves registers such as instruction pointer, stack pointer etc...
However what I don't get here is that, can't the data in the stack of the thread itself be modified between the call to setjmp and longjmp. In that case, wouldn't longjmp not work as expected.
To make it clear, for example, when longjmp restores the stack pointer, say the data in the memory the stack pointer is pointing now is not the same as was when setjmp was called. Can this happen? And if that happens, aren't we in trouble?
Also what is meant by the statement, "The longjmp() routines may not be called after the routine which called the setjmp() routines returns."
setjmp()/longjmp()
are not meant to save the stack, that's what setcontext()/getcontext()
are for.
The standard specifies that the value of non-volatile automatic variables defined in the function that calls setjmp()
that are changed between the setjmp()
and the longjmp()
calls are unspecified after a longjmp()
. There are also some restrictions on how you call setjmp()
for this same reason.