Source – SV Verification Directory
Dymanic Arrays
Dynamic arrays are arrays that do not have their size set during the compile time. They can be allocated and resized during simulation based on the design requirement so that the simulation cosumes minimal amount of memory. A dynamic array can be declare with empty word subscripts “[ ]”. The array is initially empty and must be called with the “new[ ]” constuctor to allocate space, passing in the number of entries in the square brackets.
Assignments between fixed-size and dynamic arrays are possible as long as the base type is the same.
Dynamic Array example is shown below:
module dynamic_arrays ();
int dyn1[], dyn2[];
initial begin
dyn1 = new[10];
foreach(dyn1[i]) dyn1[i] = i;
$display("-------------- Dynamic Array 1 --------------");
foreach(dyn1[i]) $display("\t dyn1[%0d] = %0d", i, dyn1[i]);
dyn2 = dyn1;
$display("-------------- Dynamic Array 2 --------------");
foreach(dyn2[i]) $display("\t dyn2[%0d] = %0d", i, dyn2[i]);
foreach(dyn2[i]) dyn2[i] = dyn2[i] + 5;
$display("-------------- Dynamic Array 2 --------------");
foreach(dyn2[i]) $display("\t dyn2[%0d] = %0d", i, dyn2[i]);
dyn2 = new[15] (dyn1);
$display("-------------- Dynamic Array 2 --------------");
foreach(dyn2[i]) $display("\t dyn2[%0d] = %0d", i, dyn2[i]);
end
endmodule