- This question is about static scoping, recursion, and higher-order procedures in the C programming language. Run the following C program through Gnu C compiler:
#include <stdio.h>
int main() { int x, y;
void p1(int y, void q(int)) { void p2(int x) { x = y + 2; printf(%d
, x); q(y);
}
if (x == y)
q(y); else p1(y+1, p2);
}
void p2(int x) { x = y + 2; printf(%d
, x);
}
x = 2; y = 2; p1(0, p2);
}
- What is the sequence of values printed?
- Draw a Scope Diagram at the point when the last value was just printed.
- Draw a Stack Diagram at the point when the last value was just printed.
Prepare your answers to parts a, b, and c in a file A2_problem1.pdf. Refer to Lecture 10 for Scope and Stack Diagrams as well as closures. Please note:
- In the Scope Diagram, for every call on p1, be sure to show the closure that is passed to q in addition to the value bound to y. It is important to correctly nest the frames for procedure calls.
- In the Stack Diagram, it suffices to show just the names of the stack frames along with the
static and dynamic links. Internal details of procedures p1 and p2 are not required.
Compiling and running a C program (say, program.c) using the Gnu C compiler under Linux:
- Compiling c: /util/bin/gcc program.c
- Executing c: ./a.out
Reviews
There are no reviews yet.