Shallow Copy and Deep Copy in SystemVerilog
In SystemVerilog, shallow copy and deep copy are used when copying objects of a class.
The difference lies in how the class properties, especially dynamic and object handles,
are copied.
1. Shallow Copy
A shallow copy only copies the object handle (reference), meaning that both the original
and copied objects point to the same memory. Any modifications to one object will reflect
in the other.
Example of Shallow Copy:
class A;
int data;
function new(int d);
this.data = d;
endfunction
endclass
module shallow_copy_test;
A obj1, obj2;
initial begin
obj1 = new(10);
obj2 = obj1; // Shallow copy: both obj1 and obj2 point to the same memory
$display("Before modification: obj1.data = %0d, obj2.data = %0d", obj1.data,
obj2.data);
obj2.data = 20; // Modifying obj2 affects obj1 as well
$display("After modification: obj1.data = %0d, obj2.data = %0d", obj1.data,
obj2.data);
end
endmodule
Output:
Before modification: obj1.data = 10, obj2.data = 10
After modification: obj1.data = 20, obj2.data = 20
Since obj1 and obj2 are referencing the same memory, modifying obj2.data changes
obj1.data as well.
2. Deep Copy
A deep copy creates a completely new object with its own memory space. Changes in the
copied object do not affect the original.
Example of Deep Copy:
class A;
int data;
function new(int d);
this.data = d;
endfunction
function A copy(); // Deep copy function
copy = new(this.data); // Creating a new instance and copying the value
endfunction
endclass
module deep_copy_test;
A obj1, obj2;
initial begin
obj1 = new(10);
obj2 = obj1.copy(); // Deep copy: obj2 is a separate object
$display("Before modification: obj1.data = %0d, obj2.data = %0d", obj1.data,
obj2.data);
obj2.data = 20; // Modifying obj2 does not affect obj1
$display("After modification: obj1.data = %0d, obj2.data = %0d", obj1.data,
obj2.data);
end
endmodule
Output:
Before modification: obj1.data = 10, obj2.data = 10
After modification: obj1.data = 10, obj2.data = 20
Since obj2 is a newly created object with its own memory, modifying obj2.data does not
change obj1.data.
Shallow vs. Deep Copy for Class Containing Dynamic Arrays
If a class has dynamic arrays, shallow copying will cause both objects to share the same
array, while deep copying will create a new array.
Example with Dynamic Array:
class A;
int data[];
function new();
data = new[3]; // Dynamic array allocation
data = '{1, 2, 3};
endfunction
function A copy(); // Deep copy function
A temp = new();
temp.data = new[data.size()]; // Allocate new memory
temp.data = data; // Copy data values
return temp;
endfunction
endclass
module test;
A obj1, obj2;
initial begin
obj1 = new();
obj2 = obj1.copy(); // Deep copy
$display("Before modification: obj1.data = {%0d, %0d, %0d}, obj2.data = {%0d, %0d,
%0d}", obj1.data[0], obj1.data[1], obj1.data[2], obj2.data[0], obj2.data[1], obj2.data[2]);
obj2.data[1] = 99; // Modifying obj2 should not affect obj1
$display("After modification: obj1.data = {%0d, %0d, %0d}, obj2.data = {%0d, %0d,
%0d}", obj1.data[0], obj1.data[1], obj1.data[2], obj2.data[0], obj2.data[1], obj2.data[2]);
end
endmodule
Output:
Before modification: obj1.data = {1, 2, 3}, obj2.data = {1, 2, 3}
After modification: obj1.data = {1, 2, 3}, obj2.data = {1, 99, 3}
Since copy() allocated a new dynamic array, modifying obj2.data does not affect
obj1.data.
Summary
• For classes with dynamic arrays or other objects, always use deep copy to avoid
unintended modifications.