procedure p(x:integer);
var y:integer; /* declaration of a variable y local to p */
procedure q; /* declaration of a procedure q local to p */
procedure r; /* declaration of a procedure r local to q */
var x:integer; /* declaration of a variable x local to r */
begin /* begin body of r */
x := 0;
q
end; /* end body of r */
begin /* begin body of q */
if x = y
then begin y := y - 1; r end
else print(y)
end; /* end body of q */
begin /* begin body of p */
y := x;
q
end; /* end body of p */
Assume that at a certain point, the main program
contains a call of the form
p(1);Show the stack snapshot (i.e. the configuration of the activation records on the stack) at the moment in which the instruction print(y) is executed for the first time. In particular, show the static and the control links. You can use a drawing.
function fibo(n:integer):integer;
begin
if n = 0 then fibo := 0
else if n = 1 then fibo := 1
else fibo := fibo(n-1) + fibo(n-2)
end;
function greatest(i:integer): integer;
/* greatest element in an array A with n+1 positions */
begin /* containing non-negative numbers (call: greatest(0)) */
if i > n then greatest := 0
else greatest := max(A[i],greatest(i+1))
end;
where max is the function declared as
function max(x,y:integer): integer;
begin
if x > y then max := x
else max := y
end;
procedure inc(i:integer); /* adds 1 to all elements of an array A with n+1 pos. */
begin
if n > i then begin
inc(i+1);
A[i] := A[i] + 1
end
end;
procedure sum(i:integer); /* adds all elements of an array A with n+1 positions */
begin
if n > 1 then begin
A[i+1] := A[i] + A[i+1];
sum(i+1)
end
end;
function last(l:list):integer; /* Last element of a non-empty list of integers */
begin /* "^" is the dereferencing operator in Pascal */
if l^.next = nil then last := l^.info
else last := last(l^.next)
end;
var p,q,r: ^integer; /* declaration of p, q and r as pointers to int */ begin new(p); p^ := 1; q := p; q^ := p^ + 1; print(p^); dispose(q); new(r); r^ := 1; p^ := p^ + 1; print(r^); end
void foo(){
int x = 5;
int *p = &x;
*p = *p + 1;
printf("%d",*p);
}
void foo(){
int x = 5;
p = &x; /* p is a global variable with a declaration of the form
int *p; */
x = x + 1;
printf("%d",*p);
}
void foo(){
int *p;
p = new int; /* this instruction corresponds to new(p) in Pascal */
*p = 5;
p = p + 1;
printf("%d",*p);
delete(p); /* this instruction corresponds to dispose(p) in Pascal */
}
void foo(){
int *p;
int **q; /* q is declared as a pointer to a pointer to int */
q = &p;
p = new int;
*p = 5;
**q = **q + 1;
printf("%d",**q);
}