Shallow Copy vs Deep Copy in SystemVerilog: new, copy() and clone()

In short: In SystemVerilog there are three ways to “copy” an object. p2 = p1; copies only the handle, so both names point to one object. p2 = new p1; is a shallow copy: a new object with every field copied, but any handle inside it still points to the same inner object. A deep copy also makes new copies of every inner object, so the two are fully independent. SystemVerilog has no built-in deep copy: you write a copy() method (or do_copy() in UVM) that copies each inner object too.

Think of it like a folder with a link inside

You keep a paper folder of notes. One page in it isn’t a note. It’s a link to a shared spreadsheet.

  • Handle copy (p2 = p1;): you tell a teammate where your folder is. One folder, two people. Whatever they scribble, you see.
  • Shallow copy (p2 = new p1;): you photocopy every page. Your teammate gets their own folder. But the page with the link gets copied too, and the link still opens the same spreadsheet.
  • Deep copy (p2 = p1.copy();): you photocopy every page, and make a copy of the spreadsheet, and write the new link on their page.

In SystemVerilog terms:

  • Plain fields (int, bit, string, enums) are the printed notes.
  • Arrays and queues of plain values are printed notes too. They get copied.
  • Class handles are the links. A shallow copy copies the link, not what it points to.
Three ways to copy: handle copy gives 1 packet and 1 payload; shallow copy gives 2 packets sharing 1 payload; deep copy gives 2 packets and 2 payloads
Figure 1. Handle copy, shallow copy and deep copy side by side.

Example 1: p2 = p1 copies the handle, not the object

module tb;
  class packet;
    int id;
    function new(int id); this.id = id; endfunction
  endclass

  initial begin
    packet p1, p2;
    p1 = new(10);
    p2 = p1;            // copy the handle, not the object
    p2.id = 20;
    $display("p1.id=%0d p2.id=%0d same object? %0d", p1.id, p2.id, p1 == p2);
    $finish;
  end
endmodule

Output (Verilator 5.020, 11 Nov 2026):

p1.id=20 p2.id=20 same object? 1
  • There is still only one packet. p1 and p2 are two names for it.
  • Changing p2.id changed what p1 sees.
  • This isn’t a copy at all. It’s the “here’s where my folder is” case.

Example 2: p2 = new p1 is a shallow copy

module tb;
  class payload;
    int data;
  endclass

  class packet;
    int     id;
    int     bytes[$];     // a queue lives inside the object
    payload pl;           // a handle to another object
    function new();
      $display("  new() ran");
      pl = new();
    endfunction
  endclass

  initial begin
    packet p1, p2;
    $display("make p1:");
    p1 = new();
    p1.id = 10; p1.bytes = '{1, 2}; p1.pl.data = 5;

    $display("shallow copy p2 = new p1:");
    p2 = new p1;        // shallow copy

    p2.id       = 20;   // plain field
    p2.bytes[0] = 99;   // queue element
    p2.pl.data  = 30;   // field of the nested object

    $display("p1: id=%0d bytes[0]=%0d pl.data=%0d", p1.id, p1.bytes[0], p1.pl.data);
    $display("p2: id=%0d bytes[0]=%0d pl.data=%0d", p2.id, p2.bytes[0], p2.pl.data);
    $display("same payload object? %0d", p1.pl == p2.pl);
    $finish;
  end
endmodule

Output (Verilator 5.020, 11 Nov 2026):

make p1:
  new() ran
shallow copy p2 = new p1:
p1: id=10 bytes[0]=1 pl.data=30
p2: id=20 bytes[0]=99 pl.data=30
same payload object? 1

Four things to notice:

  • **id is its own copy.** p2.id = 20 left p1.id at 10.
  • The queue is its own copy too. p2.bytes[0] = 99 left p1.bytes[0] at 1. Arrays and queues of plain values are values, not handles.
  • The payload is shared. p2.pl.data = 30 also changed p1. Both pl handles point to the one payload.
  • **new() did not run for p2.** “new() ran” prints once, for p1 only. A shallow copy copies fields; it never calls your constructor. So anything your constructor does (counters, IDs, creating objects) is skipped.
Example 2 result: p1 and p2 each have their own id and bytes queue, but both pl handles point to one payload whose data is now 30
Figure 2. Example 2: own id and queue, one shared payload.

Example 3: a deep copy with your own copy() method

module tb;
  class payload;
    int data;
    function payload copy();
      copy = new();
      copy.data = data;
    endfunction
  endclass

  class packet;
    int     id;
    payload pl;
    function new(); pl = new(); endfunction
    function packet copy();
      copy    = new();        // fresh packet (and a fresh payload from new())
      copy.id = id;
      copy.pl = pl.copy();    // go one level down: copy the payload too
    endfunction
  endclass

  initial begin
    packet p1, p2;
    p1 = new(); p1.id = 10; p1.pl.data = 5;
    p2 = p1.copy();           // deep copy
    p2.id = 20; p2.pl.data = 30;
    $display("p1: id=%0d pl.data=%0d", p1.id, p1.pl.data);
    $display("p2: id=%0d pl.data=%0d", p2.id, p2.pl.data);
    $display("same payload object? %0d", p1.pl == p2.pl);
    $finish;
  end
endmodule

Output (Verilator 5.020, 11 Nov 2026):

p1: id=10 pl.data=5
p2: id=20 pl.data=30
same payload object? 0
  • p2 has its own payload now. Changing it left p1 at 5.
  • The trick is one copy() per class. packet::copy() copies its own fields, then calls pl.copy() to go one level down.
  • If payload held a handle too, payload::copy() would call that object’s copy(). Each class copies its own level. That’s what makes it deep.
Example 3 result: p1 and p2 are separate packets, each pointing to its own payload; p1 payload data stays 5 while p2 is 30
Figure 3. Example 3: nothing is shared.

Handle copy vs shallow copy vs deep copy

Handle copyShallow copyDeep copy
Codep2 = p1;p2 = new p1;p2 = p1.copy();
New object?NoYes, oneYes, one per level
Plain fieldsSame object, so sharedCopiedCopied
Arrays and queues of valuesSharedCopiedCopied
Inner objects (handles)SharedSharedCopied
Your new() runs?NoNoYes, if copy() calls new()
Built in?YesYesNo, you write it
Folder pictureTelling someone where your folder isPhotocopy; same spreadsheet linkPhotocopy plus a copy of the spreadsheet

Working example 4: copying a queue of handles

A queue of class handles is a queue of links. Copying the queue copies the links.

module tb;
  class packet;
    int id;
    function new(int id); this.id = id; endfunction
    function packet copy(); copy = new(id); endfunction
  endclass

  initial begin
    packet p, q1[$], q2[$], q3[$];
    p = new(1); q1.push_back(p);
    p = new(2); q1.push_back(p);

    q2 = q1;                          // copies the queue, not the packets
    foreach (q1[i]) q3.push_back(q1[i].copy());   // copies each packet

    q2[0].id = 50;                    // change through q2
    q3[1].id = 70;                    // change through q3

    $display("q1: %0d %0d", q1[0].id, q1[1].id);
    $display("q2: %0d %0d", q2[0].id, q2[1].id);
    $display("q3: %0d %0d", q3[0].id, q3[1].id);
    q2.delete();
    $display("after q2.delete(): q1 size=%0d", q1.size());
    $finish;
  end
endmodule

Output (Verilator 5.020, 11 Nov 2026):

q1: 50 2
q2: 50 2
q3: 1 70
after q2.delete(): q1 size=2
  • q2 = q1 made a new queue, but filled it with the same two handles. q2[0].id = 50 shows up in q1.
  • q2.delete() emptied q2 only. The queue itself was copied; the packets weren’t.
  • q3 holds new packets from copy(), so q3[1].id = 70 never reaches q1.
Example 4: q2 = q1 makes both queues point to the same two packets, so q2[0].id = 50 shows in q1; q3 holds new packets made with copy(), so q3[1].id = 70 does not
Figure 4. Example 4: copying a queue copies the handles, not the packets.

Working example 5: copying through a base class handle

module tb;
  class base_pkt;
    int id;
    virtual function base_pkt clone();      // every class makes its own kind
      base_pkt c = new();
      c.id = id;
      return c;
    endfunction
  endclass

  class crc_pkt extends base_pkt;
    int crc;
    virtual function base_pkt clone();
      crc_pkt c = new();
      c.id = id; c.crc = crc;
      return c;
    endfunction
  endclass

  initial begin
    crc_pkt  orig, back;
    base_pkt b, s, d;
    orig = new(); orig.id = 7; orig.crc = 'hAB;
    b = orig;                 // base handle, crc_pkt object

    s = new b;                // shallow copy through the base handle
    $display("new b   -> crc_pkt? %0d", $cast(back, s));

    d = b.clone();            // virtual clone
    if ($cast(back, d)) $display("clone() -> crc_pkt? 1, id=%0d crc=%0h", back.id, back.crc);
    $finish;
  end
endmodule

Output (Verilator 5.020, 11 Nov 2026):

new b   -> crc_pkt? 0
clone() -> crc_pkt? 1, id=7 crc=ab
  • s = new b; builds an object of the type of the variable on the left, base_pkt. The crc field has nowhere to live, so it’s gone. $cast to crc_pkt fails.
  • b.clone() is virtual, so the object’s own clone() runs. You get a real crc_pkt back, crc and all.
  • Picture photocopying a folder through a template that only has slots for the base pages. The extra pages don’t fit, so they don’t come out.
Example 5: shallow copy new b through a base handle makes a base_pkt and loses crc; the virtual clone() makes a crc_pkt with id 7 and crc ab
Figure 5. Example 5: new through a base handle vs a virtual clone().

This is why UVM’s clone() exists. More on virtual methods in virtual vs pure virtual methods.

Working example 6: copy() vs clone() in UVM

In UVM you don’t write copy() or clone() yourself. You write do_copy(), and UVM’s copy() and clone() call it.

`include "uvm_macros.svh"
import uvm_pkg::*;

class payload extends uvm_object;
  `uvm_object_utils(payload)
  int data;
  function new(string name = "payload"); super.new(name); endfunction
  function void do_copy(uvm_object rhs);
    payload r;
    super.do_copy(rhs);
    $cast(r, rhs);
    data = r.data;
  endfunction
endclass

class packet extends uvm_object;
  `uvm_object_utils(packet)
  int     id;
  payload pl;
  function new(string name = "packet");
    super.new(name);
    pl = payload::type_id::create("pl");
  endfunction
  function void do_copy(uvm_object rhs);
    packet r;
    super.do_copy(rhs);
    $cast(r, rhs);
    id = r.id;
    pl.copy(r.pl);          // deep: copy into our own payload
    // pl = r.pl;           // shallow: would share r's payload
  endfunction
endclass

module tb;
  initial begin
    packet p1, p2, p3;
    p1 = packet::type_id::create("p1");
    p1.id = 10; p1.pl.data = 5;

    p2 = packet::type_id::create("p2");
    p2.copy(p1);                       // copy INTO an object you already have
    $cast(p3, p1.clone());             // clone = create + copy, returns a new object

    p2.pl.data = 30;
    p3.pl.data = 40;
    $display("p1.pl.data=%0d p2.pl.data=%0d p3.pl.data=%0d", p1.pl.data, p2.pl.data, p3.pl.data);
    $display("p3 name=%s, same payload as p1? %0d", p3.get_name(), p3.pl == p1.pl);
  end
endmodule

Output (Synopsys VCS 2025.06, UVM 1.2, 12 Nov 2026):

p1.pl.data=5 p2.pl.data=30 p3.pl.data=40
p3 name=p1, same payload as p1? 0
  • All three payloads are separate. p2.pl.data = 30 and p3.pl.data = 40 left p1 at 5, because do_copy() copies the payload instead of sharing it.
  • p3 is named p1. clone() creates the new object with the original’s name, so rename it with set_name() if your logs need to tell them apart.
  • p2.copy(p1) copies into an object you already have.
  • p1.clone() creates a new object through the factory, then copies into it. It returns a uvm_object, so you $cast it back.
  • Deep or shallow is your choice inside do_copy(): pl.copy(r.pl) is deep; pl = r.pl would be shallow.
copy()clone()
Makes a new object?No, fills one you already madeYes, with create()
ReturnsNothingA uvm_object (needs $cast)
Deep or shallow?Whatever your do_copy() doesWhatever your do_copy() does
Typical useReuse an object in a loopKeep a snapshot, e.g. in a monitor before write()

That snapshot matters in a real testbench. A monitor that writes the same object every time leaves the scoreboard reading only the last value; mistake 1 in UVM TLM explained shows it on VCS with three fixes.

Common mistakes (and the fix for each)

These come from real Verification Academy threads, retold in my own words with links.

1. Calling new inside copy() for a nested object

  • What happened: in 2018 an engineer wrote a copy() that did this.in = new ou.in; for the inner object and wasn’t sure how arrays inside it got copied (Verification Academy, 2018).
  • Why: new ou.in is only a shallow copy of the inner object. It works for one level, then breaks the moment the inner object holds a handle of its own.
  • Fix: Dave Rich’s advice in that thread: give every class its own copy() and call down, like this.in.copy(from.in). Arrays of plain values just need =, as example 2 shows.

2. Copying a queue of objects with one assignment

  • What happened: an engineer had a UVM transaction holding a queue of objects. obj_queue = rhs_.obj_queue in do_copy() left both transactions sharing the same objects (Verification Academy).
  • Why: the queue gets copied, but its entries are handles. Same as example 4.
  • Fix: loop over the queue and clone() each entry, with a $cast back to the element type. In that thread, a pure virtual do_copy() in the element’s base class also had to go before the clone worked.

3. Thinking a base-type return throws away the child’s fields

  • What happened: in 2021 someone reading Chris Spear’s book worried that a child class’s copy(), which returns a base-class handle, would lose the child-only bad_crc field (Verification Academy, 2021).
  • Why: the object is still the child type. Only the handle is the base type. Dave Rich confirmed the field is kept; you just can’t reach it through the base handle.
  • Fix: $cast the result back to the child type, as example 5 does. What does lose the field is new through a base handle, also in example 5.

4. Expecting a base handle to see the child’s version of a field

  • What happened: in 2020 an engineer had a field A in both the base and the derived class. After base_h = derived_h, reading base_h.A gave the base value, not the derived one (Verification Academy, 2020).
  • Why: base_h = derived_h is a handle copy: no new object. And fields aren’t virtual. The derived object holds two As, and a base handle sees the base one.
  • Fix: don’t reuse field names in a child class. Use a virtual method when the child needs to change behaviour.

5. Overriding clone() or copy() in UVM

  • What happened: an engineer asked why clone() matters and tried overriding it, which hit a compile error because the argument didn’t match the base method (Verification Academy).
  • Why: in UVM, copy() and clone() are the framework’s. clone() already does create() plus copy(), and copy() calls your do_copy().
  • Fix: override do_copy() only, like example 6. Call super.do_copy(rhs) first so the parent’s fields come along.

6. Not knowing what the field macros copy

  • What happened: in 2018 an engineer asked whether the UVM field macros give a shallow or a deep copy (Verification Academy, 2018).
  • Why: Dave Rich’s answer: the uvm_field_object macro uses clone(), which normally means deep. He also doesn’t recommend the field macros.
  • Fix: write do_copy() by hand. Then you can see exactly which handles are deep-copied and which are shared.

Every idea on this page, in one picture

IdeaPicture it as
ObjectA paper folder of notes
HandleKnowing where the folder is
Plain field, array or queue of valuesA printed note in the folder
Handle inside the objectA page with a link to a shared spreadsheet
p2 = p1;Telling a teammate where your folder is
p2 = new p1;Photocopying every page; the link still opens the same spreadsheet
Deep copyPhotocopying the pages and copying the spreadsheet too
Constructor skipped by new p1The photocopier doesn’t fill in a fresh cover sheet
new through a base handlePhotocopying through a template with too few slots
Virtual clone()Asking the folder’s owner to make the copy
UVM copy()Photocopying into a folder you already have
UVM clone()Getting a brand-new folder and photocopying into it

FAQ

What is the difference between shallow copy and deep copy in SystemVerilog?

A shallow copy (new p1) makes a new object and copies its fields, but inner objects stay shared. A deep copy also copies every inner object.

Does SystemVerilog have a built-in deep copy?

No. You write a copy() method per class, or do_copy() in UVM.

Is p2 = p1 a copy?

No. It copies the handle, so both names point to one object.

Does new p1 call the constructor?

No. It copies fields only, so code in new() doesn’t run.

Are arrays and queues copied by a shallow copy?

Yes, if they hold plain values. If they hold class handles, the handles are copied, not the objects.

What is the difference between copy() and clone() in UVM?

copy() fills an object you already have. clone() creates a new one and copies into it. Both use your do_copy().

References

  • IEEE 1800-2017, SystemVerilog standard: section 8.12 (assignment, renaming and copying), 8.13 (inheritance), 8.16 (casting) and 8.20 (virtual methods).
  • Accellera UVM 1.2 class reference: uvm_object::copy(), clone() and do_copy().
  • Verification Academy forum threads linked in the mistakes section (accessed 11 Nov 2026).
  • Examples 1 to 5 tested on Verilator 5.020 on 11 Nov 2026. Example 6 on Synopsys VCS 2025.06 with UVM 1.2 on 12 Nov 2026.

What to read next

One Comment

  1. There is a typo mistake in below mentioned paragraph.
    Actually class B instantiates another Class i.e. A. It means Class B is having two variables inside i.e. integer i and class variable a of type Class “A”.
    Plz update it in below:
    Here we are having two Classes named “Class A” & “Class B“. Class A instantiates another Class i.e. B. It means, Class A is having two variables inside i.e. integer variable i and Class variable a of type Class “A”

Comments are closed.