CEIS420 Week 5 ILab Solution
CEIS420 Week 5 ILab Solution
Name:
procedure Bigsub is
procedure A is
procedure B is
begin - - of B
. . . <---------------------- 1
end; --- of B
procedure C is
begin - - of C
...
B;
...
end; -- of C
begin -- of A
...
C;
...
end; -- of A
begin -- of Bigsub
...
A;
...
end; -- of Bigsub
Answer:
dynamic link
ari for B static link
return (to C)
dynamic link
ari for C static link
return (to A)
dynamic link
ari for A static link
return (to BIGSUB)
dynamic link
ari for static link
BIGSUB
return
.
.
stack
Question 2 (20 points)
Show the stack with all activation record instances, including the dynamic chains, when
execution reaches Position 1 in the following skeletal program. This program uses the deep-
access method to implement dynamic scoping.
procedure Bigsub is
procedure A (Flag : Boolean) is
procedure B is
...
A (false);
end; - - of B
begin - - of A
if flag
then B;
else C;
...
end; - - of A
procedure C is
procedure D is
. . . <------------------------ 1
end; - - of D
...
D;
end; - - of C
begin - - of Bigsub
...
A(true);
...
end; - - of Bigsub
Bigsub calls A
A calls B
B calls A
A calls C
C calls D
Answer:
dynamic link
ari for D static link
return (to C)
dynamic link
ari for C static link
return (to A)
parameter (flag)
ari for A dynamic link
static link
return (to B)
dynamic link
ari for B
static link
return ( to A)
parameter (flag)
dynamic link
ari for A static link
return (BIGSUB)
ari for dynamic link
BIGSUB static link
return (to caller)
stack
Question 3 (10 points)
Suppose someone designed a stack abstract data type in which the function, top, returned an
access path (or pointer) rather than retuning a copy of the top element. This is not a true data
abstraction. Why? Give an example that illustrates the problem.
Answer:
The problem with this is that the user is given access to the stack through the returned value of
the top function. For example, if p is a pointer to objects of the type stored in the stack, we could
have
p = top(stack1);
*p = 42;
These statements access the stack directly, which violates the principle of a data abstraction.