SystemVerilog dynamic arrays


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

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;

Iterate over elements of dynamic array

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

Welcome to the