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.
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.
p1andp2are two names for it. - Changing
p2.idchanged whatp1sees. - 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:
- **
idis its own copy.**p2.id = 20leftp1.idat 10. - The queue is its own copy too.
p2.bytes[0] = 99leftp1.bytes[0]at 1. Arrays and queues of plain values are values, not handles. - The payload is shared.
p2.pl.data = 30also changedp1. Bothplhandles 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 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
p2has its own payload now. Changing it leftp1at 5.- The trick is one
copy()per class.packet::copy()copies its own fields, then callspl.copy()to go one level down. - If
payloadheld a handle too,payload::copy()would call that object’scopy(). Each class copies its own level. That’s what makes it deep.

Handle copy vs shallow copy vs deep copy
| Handle copy | Shallow copy | Deep copy | |
|---|---|---|---|
| Code | p2 = p1; | p2 = new p1; | p2 = p1.copy(); |
| New object? | No | Yes, one | Yes, one per level |
| Plain fields | Same object, so shared | Copied | Copied |
| Arrays and queues of values | Shared | Copied | Copied |
| Inner objects (handles) | Shared | Shared | Copied |
Your new() runs? | No | No | Yes, if copy() calls new() |
| Built in? | Yes | Yes | No, you write it |
| Folder picture | Telling someone where your folder is | Photocopy; same spreadsheet link | Photocopy 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 = q1made a new queue, but filled it with the same two handles.q2[0].id = 50shows up inq1.q2.delete()emptiedq2only. The queue itself was copied; the packets weren’t.q3holds new packets fromcopy(), soq3[1].id = 70never reachesq1.
![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](https://theartofverification.com/wp-content/uploads/2026/11/copy-queue-ex4.webp)
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. Thecrcfield has nowhere to live, so it’s gone.$casttocrc_pktfails.b.clone()is virtual, so the object’s ownclone()runs. You get a realcrc_pktback,crcand 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.

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 = 30andp3.pl.data = 40leftp1at 5, becausedo_copy()copies the payload instead of sharing it. p3is named p1.clone()creates the new object with the original’s name, so rename it withset_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 auvm_object, so you$castit back.- Deep or shallow is your choice inside
do_copy():pl.copy(r.pl)is deep;pl = r.plwould be shallow.
copy() | clone() | |
|---|---|---|
| Makes a new object? | No, fills one you already made | Yes, with create() |
| Returns | Nothing | A uvm_object (needs $cast) |
| Deep or shallow? | Whatever your do_copy() does | Whatever your do_copy() does |
| Typical use | Reuse an object in a loop | Keep 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 didthis.in = new ou.in;for the inner object and wasn’t sure how arrays inside it got copied (Verification Academy, 2018). - Why:
new ou.inis 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, likethis.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_queueindo_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$castback to the element type. In that thread, a pure virtualdo_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-onlybad_crcfield (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:
$castthe result back to the child type, as example 5 does. What does lose the field isnewthrough 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
Ain both the base and the derived class. Afterbase_h = derived_h, readingbase_h.Agave the base value, not the derived one (Verification Academy, 2020). - Why:
base_h = derived_his a handle copy: no new object. And fields aren’t virtual. The derived object holds twoAs, 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()andclone()are the framework’s.clone()already doescreate()pluscopy(), andcopy()calls yourdo_copy(). - Fix: override
do_copy()only, like example 6. Callsuper.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_objectmacro usesclone(), 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
| Idea | Picture it as |
|---|---|
| Object | A paper folder of notes |
| Handle | Knowing where the folder is |
| Plain field, array or queue of values | A printed note in the folder |
| Handle inside the object | A 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 copy | Photocopying the pages and copying the spreadsheet too |
Constructor skipped by new p1 | The photocopier doesn’t fill in a fresh cover sheet |
new through a base handle | Photocopying 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()anddo_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
- Polymorphism in SystemVerilog: why
clone()must be virtual - Virtual vs pure virtual methods
- create() vs new(): what
clone()uses under the hood
Part of the learning path SystemVerilog for verification

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”