0% found this document useful (0 votes)
94 views

CEIS420 Week 5 ILab Solution

When execution reaches Position 1: 1. The stack will contain activation record instances for procedures D, C, A, B, and Bigsub, linked by dynamic chains. 2. Procedure D is at the top of the stack, with static links leading down to C, A, B, and Bigsub's activation records. 3. Returning a pointer from the top function instead of a copy violates data abstraction by giving direct access to modify the stack.

Uploaded by

Amit Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

CEIS420 Week 5 ILab Solution

When execution reaches Position 1: 1. The stack will contain activation record instances for procedures D, C, A, B, and Bigsub, linked by dynamic chains. 2. Procedure D is at the top of the stack, with static links leading down to C, A, B, and Bigsub's activation records. 3. Returning a pointer from the top function instead of a copy violates data abstraction by giving direct access to modify the stack.

Uploaded by

Amit Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CEIS 420 Week 5 iLab

Name:

Question 1 (20 points)


Show the stack with all activation record instances, including static and dynamic chains, when
execution reaches Position 1 in the following skeletal program. Assume that Bigsub is at Level 1.

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

The calling sequence for this program for execution to reach D is

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.

You might also like