Skip to content

SystemVerilog Dynamic Arrays Cheatsheet

This is a handy cheatsheet of what you can do with SystemVerilog dynamic arrays

Examples on EDA playground

Initializing a dynamic array

logic [7:0] darray1[] = '{'d1, 'd2, 'd3};

Get size of dynamic array

$display("darray1 size %0d", darray1.size());

Resizing a dynamic array

logic [7:0] darray1[] = '{'d1, 'd2, 'd3};

initial begin
darray1 = new[10](darray1); // copy over old contents and resize
end

Copying over a dynamic array to another fixed or dynamic array

logic [7:0] darray1[] = '{'d1, 'd2, 'd3};
logic [7:0] darray2[];
logic [7:0] farray1[3];

initial begin
darray2 = darray1; 
// or
darray2 = new[darray1.size()](darray1);

// assignment to fixed array of same size
farray = darray1;
end

Iterate over elements of dynamic array

foreach (darray1[i]) begin
$display("element %0d = 0x%x", i, darray1[i]);
end


Subscribe

Every month or so I send out a newsletter with lessons from my experience, notable technical papers, and notifications about new articles.

If you found this content useful then please consider supporting this site! 🫶

Buy Me A Coffee