Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutRoadmapCareersInternshipsFAQContactPrivacyTerms
Skip to lesson

Course contents

  • How to Use This Puzzle Bank
  • SystemVerilog Language Puzzles25
  • Difficulty Guide
  • Full Topic Map
  • Interview-Day Tips
  • Study Route: One Week Before the Interview
  • Study Route: Two Weeks
  • Study Route: Four Weeks (Full Course)4
  • OOP & Testbench Puzzles25
    • Handle and Copy Puzzles
      • Shallow Copy in Nested Payload
      • Queue of Handle Cells Still Aliases
      • Analysis Port Lifetime Reuse
      • Null Handle During Partial Deep Copy
      • Handle Equality vs Content Equality
      • Clone Without Overridden do_copy
    • Virtual Method Puzzles
    • Parameterized Class Puzzles
    • Factory Override Puzzles
    • OOP & Testbench Puzzles: Tricky Q&A
  • Concurrency & Timing Puzzles25
  • Randomization & Constraint Puzzles25
  • SVA Assertion Puzzles25
  • Functional Coverage Puzzles25
  • Logic & Aptitude Puzzles25
  • Scenario & Debug Puzzles25
  • UVM Testbench Puzzles25
  • Clock, Reset & CDC Puzzles25
  • Bus and Interface Protocol Puzzles25
  • Interview Prep: Puzzle Rounds7
  • DV Puzzle Cheatsheet
Loading lesson

←Handle and Copy Puzzles
  1. Topics
  2. DV Interview Puzzles
  3. OOP & Testbench Puzzles
  4. Handle and Copy Puzzles
  5. Queue of Handle Cells Still Aliases
On this page

    VLSI DV Interview PuzzlesAll levels

    Queue of Handle Cells Still Aliases

    A sequence item stores a queue of class-handle cells. Why does `dst.cells[0].v` change when only `src` is modified after copy?

    Puzzle

    Difficulty: Medium · Puzzle 2 of 6 · Topic: Handle and Copy Puzzles

    A sequence item stores a queue of class-handle cells. Why does `dst.cells[0].v` change when only `src` is modified after copy?

    Code

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    class cell extends uvm_object;
      `uvm_object_utils(cell)
      int v;
      function new(string name="cell"); super.new(name); endfunction
    endclass
    
    class tr extends uvm_sequence_item;
      `uvm_object_utils(tr)
      cell cells[$];
      function new(string name="tr");
        super.new(name);
        cells.push_back(cell::type_id::create("c0"));
        cells.push_back(cell::type_id::create("c1"));
      endfunction
      function void do_copy(uvm_object rhs);
        tr r;
        if (!$cast(r, rhs)) return;
        cells = r.cells;
      endfunction
    endclass
    
    initial begin
      tr src = tr::type_id::create("src");
      tr dst = tr::type_id::create("dst");
      src.cells[0].v = 5;
      dst.copy(src);
      src.cells[0].v = 99;
      $display("src=%0d dst=%0d", src.cells[0].v, dst.cells[0].v);
    end

    Hint

    Queue assignment copies queue structure, but inspect the element type.

    Step-by-step solution

    Diagram
    1) `cells` is a queue of class handles, not packed values.
    2) `cells = r.cells` duplicates queue indices but each element still points to the same `cell` objects.
    3) Mutating `src.cells[0].v` mutates the shared object referenced by both queues.
    4) Deep-copy by resizing `cells` and cloning each element in a loop.

    Answer

    Answer: Because queue assignment is shallow for class-handle elements; both queues share identical `cell` objects.

    How this was checked

    Checked by running the code in Verilator 5; the printed output supports the answer above. A hand-written model of the copy flow (not the UVM library): assigning a queue of handles shares the cells, a per-element new keeps them apart.

    Why candidates get it wrong

    People treat class-handle queues like queues of ints and assume assignment implies value-copy.

    Interviewer follow-up

    How would you deep-copy while preserving element dynamic type if `cell` has subclasses? Model answer: loop over `r.cells`, and for each element do `$cast(c, r.cells[i].clone()); cells.push_back(c);` (after `cells.delete()`). `clone()` calls the element's own `create()`, so a subclass instance stays a subclass instance; `cell::type_id::create` + `copy` would always build a plain `cell`.

    Say it out loud

    The destination changes because queue assignment copies the handles, not the cell objects they point to. So both queues hold the same cell objects, and mutating one through src shows up through dst. The trap is assuming that assigning a queue duplicates its contents like it does for ints. I would resize the destination queue and clone each element in a loop, then check that the handles differ.

    Related topics

    • Shallow vs Deep Copy

    • Handles vs Objects

    • Back to Handle and Copy Puzzles

    • OOP & Testbench Puzzles section

    Previous

    37 / 293

    Next: Analysis Port Lifetime Reuse

    Up next

    Up nextAnalysis Port Lifetime Reuse

    Sign in to save progress

    Practice this lessonPrevious lessonReport an error

    See also

    • Null Handle During Partial Deep Copy
    • Handle Equality vs Content Equality
    • Clone Without Overridden do_copy