Shallow Copy

Problem Description

Implement copy() so it returns a new Packet —

but the hdr inside the copy should point to the SAME Header

object as the original. Do NOT create a new Header.

Your copy() must:

1. Return a new Packet (not the same handle as this)

2. Copy id directly

3. Assign hdr by handle — tmp.hdr = this.hdr

Example:

Original p1:

id = 1

hdr → seq_id = 10, protocol = "AXI"

Call: p2 = p1.copy()

p2 now has:

id = 1

hdr → same Header object as p1

Now change p2.hdr.seq_id to 99.

p2.hdr.seq_id = 99 ✅ changed

p1.hdr.seq_id = 99 ⚠️ also changed!

This happens because p1 and p2 share the same Header.

Changing it from one side affects the other.

This is the key difference from Deep Copy:

Shallow Copy → p1.hdr and p2.hdr point to the SAME object

Deep Copy → p1.hdr and p2.hdr are two SEPARATE objects

SystemVerilog

Code Editor Access

Log in to write, run and submit your solution to this interview question. It's free.

Output

Output goes here...