Run-Time Environment Mistakes: 7 Mark-Losing Errors and Exact Fixes

Definitions are not enough when a trace depends on storage duration, calling order, lexical nesting and frame-address rules. Repair seven common errors through small, checkable examples.

KnowledgeGate Team

Exam prep & CS education

Updated 3 Nov 20265 min read
Run-Time Environment Mistakes: 7 Mark-Losing Errors and Exact Fixes

You may know the definitions of stack, heap, activation record, control link and access link, yet still lose marks by importing an unstated frame layout or tracing the wrong relationship. The repair is not another glossary. It is seven small, checkable examples. Before calculating, first write down four conventions from the question: storage duration, calling order, lexical nesting and frame-address rule. Never assume that one textbook activation-record diagram is universal.

1. Mistake: Putting a pointer and its target in the same storage area

Why it happens: Students classify pointers by targets. What goes wrong: They put local p on the heap or make its object die with the callee.

Consider int g = 7; int *make(int n) { int a = 3; int *p = malloc(sizeof *p); *p = n + a; return p; }. During make(5), static storage has g = 7; its frame has n = 5, a = 3 and p = 0x5000; the heap cell contains 8. 0x5000 is illustrative. After return, the frame is gone, but caller r = 0x5000 reaches the cell until freed. The run-time environment guide explains the underlying allocation, activation-record, and link model; this example isolates the pointer-versus-target mistake and its lifetime test.

What to do instead: Separate pointer and target by value, area and lifetime.

2. Mistake: Drawing one activation record per procedure instead of per active call

Why it happens: Students confuse an activation-record format with an instance. What goes wrong: Recursive calls overwrite active frames on paper, breaking parameters and returns.

Trace fact(3), with fact(1) = 1. Before the base call returns, three frames exist from bottom to top: fact(3): n = 3, fact(2): n = 2, and fact(1): n = 1. Unwind as 1, then 2 * 1 = 2, then 3 * 2 = 6. Return addresses may identify the same source-level continuation, but each live call still owns a saved slot.

The activation records and stack frames guide develops the full call-to-return trace and frame layout; this example isolates the one-frame-per-active-call mistake.

What to do instead: Add a frame at every call. Remove only that call's frame at return, never one still active.

Activation records show dynamic caller links versus static lexical links for Q's lookup of x and y.

Why it happens: Both links point to frames, encouraging "go to the previous frame". What goes wrong: Non-local lookup follows callers instead of lexical parents.

Main, lexical level 0, has x = 10; inner P, level 1, has y = 20; siblings Q and R inside P are level 2. Q prints x + y; R calls Q. In Main -> P -> R -> Q, Q's dynamic link points to caller R, but its static link points to lexical parent P. One static hop, Q -> P, finds y = 20; two, Q -> P -> Main, find x = 10. Q prints 10 + 20 = 30.

What to do instead: Write dynamic link = caller frame and static link = most recent frame of lexical parent. Count lexical levels, not call depth.

Diagram of four activation records showing caller and lexical-parent links from Q to P and Main, with x + y evaluating to 30.

4. Mistake: Inferring offset signs from stack-growth direction alone

Why it happens: "The stack grows downward" becomes "every frame offset is negative". What goes wrong: Ignoring the base-register convention produces the wrong address.

Given byte addressing, FP = 1000, local t at offset -12, and argument n at offset +8, addr(t) = 1000 + (-12) = 988, while addr(n) = 1000 + 8 = 1008. Under another convention, frame base 968 is the low address, so a slot at 988 has offset 988 - 968 = +20. Downward growth determines neither sign.

What to do instead: Use the stated rule, effective address = base + signed offset. If no layout is given, invent nothing.

Why it happens: All three fields can hold addresses. What goes wrong: The return address targets the callee or performs lexical lookup.

On an abstract machine, a 4-byte call at 100 transfers to P at 400. Its saved return address is 100 + 4 = 104, the next caller instruction, not 400. If the caller's frame pointer is 1000, the control or dynamic link records it; the access or static link supports lexical lookup. Return restores caller state and resumes at program counter 104.

What to do instead: Assign each field one purpose: resume instruction, caller frame or lexical environment. Do not infer field order.

6. Mistake: Solving copy-in/copy-out as if it were call by reference

Why it happens: Both can change an actual after a call. What goes wrong: Skipping aliases and copy-out order makes the final value a guess.

Start with a = 3 and call p(a, a). Both formals are value-result, with body x = x + 1; y = x + y;. Copy-in makes private x = 3, y = 3. The body gives x = 3 + 1 = 4, then y = 4 + 3 = 7. Left-to-right copy-out writes x then y, leaving a = 7; right-to-left writes y then x, leaving a = 4. Under reference, both names alias a: first a = 4, then a = 4 + 4 = 8. Compare the call-by-value and reference questions.

What to do instead: Draw copy-in, body and copy-out rows. Circle repeated actuals and follow the stated order.

7. Mistake: Using scope to decide an object's lifetime

Why it happens: Name visibility is confused with object lifetime. What goes wrong: Dangling and surviving objects are misclassified.

In int *bad(void) { int x = 9; return &x; }, x ends with the call, so the returned address is dangling. In int *make(void) { int *q = malloc(sizeof *q); *q = 9; return q; }, local pointer q ends, but the heap object containing 9 survives until freed. In int next(void) { static int c = 2; return ++c; }, successive calls return 3 and 4 because the same static object survives.

What to do instead: Ask separately about name visibility and object lifetime. Decide lifetime from storage duration, not scope.

8. The 45-second run-time environment error audit

Audit: (1) pointer versus target; (2) one frame per call; (3) lexical levels; (4) dynamic versus static links; (5) base plus offset; (6) resume, caller and lexical links; (7) copy-in/body/copy-out. Check fact(3) = 6, Q prints 30, and left-to-right value-result leaves a = 7.

Short version: trace the given convention, not a universal frame. Use the Compiler Design course to rebuild; use the GATE CS & IT 2027 Guidance by Sanchit Sir for timed practice; and use the Compiler Design learning module for guided revision.