1
$\begingroup$

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?

Jörg W Mittag
6,67327 silver badges26 bronze badges
asked Jun 29, 2024 at 22:01
$\endgroup$

1 Answer 1

3
$\begingroup$

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.

answered Jun 30, 2024 at 0:48
$\endgroup$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.