GATE / CS ARCHIVE

GATE CSE 2002 / QUESTION 1.18

Copy-restore and reference parameters

Compiler Design · Runtime Environments & Parameter Passing · Full GATE CSE 2002 paper

GATE 2002, Q1.18 1 mark · MCQ
Report issue

Runtime Environments & Parameter Passing

Question 1.18, GATE 2002: Compare function results under value-result and reference parameter passing. They:
(A) Never differ; (B) Differ when loops are present; (C) Differ in every case; (D) May differ when exceptions are present.
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Step 1: Use an exceptional exit to distinguish parameter mechanisms.

Step 2: Suppose an actual variable initially holds 0 and the function sets its formal to 1 before raising an exception. Reference passing has already changed the actual to 1. Under copy-restore with copying back only at normal return, the actual remains 0. This is a possible difference, not a claim that all calls differ or that exceptions have one universal language-independent copy-out policy.

Conclusion: With reference passing, updates immediately affect the actual argument. With value-result (copy-in/copy-out), they can remain in the local formal until normal return. If an exception exits before copy-out, the visible results can differ, D, under the conventional no-copy-out-on-exception rule. Aliasing can also distinguish these mechanisms; loops alone neither force nor exclude a difference.

Original paper, page 2