I am stumped on Engineering a Compiler, 3rd ed. Section 6.3, Review Question 1. The book uses the term 'activation record' to refer to a generalized procedure frame. The question asks:
In C, '
setjmp
' and 'longjmp
' provide a mechanism for inter-procedural transfer of control. 'setjmp
' builds a structure to encapsulate the runtime environment; invoking 'longjmp
' on that environment restores the environment and lets execution continue as if the most recent 'setjmp
' had just returned. What information must 'setjmp
' preserve? How does the implementation of 'setjmp
' change between stack-allocated and heap-allocated activation records?
I suspect that this is a trick question. I understand that 'setjmp
' need only save the register values (data registers, stack/frame pointers, program counter, activation record pointer etc.) in a buffer. So, the implementation could be the same, right? A procedure call keeps previous activation records intact, so a pointer to it can be saved. And restoration of the activation record pointer in 'longjmp
' would allow for access of all local variables in that record. The only difference is that a stack pointer can not be used to obtain the address of as many entities. But the activation record pointer works the same either way. Is my reasoning correct? Or is there something I am missing?
1 Answer 1
Imagine that "heap allocated" stack frames use something like malloc/free to allocate and destroy stack frame. In that case, longjmp must free all stack frames that have been allocated and not freed since you called setjmp.