Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Dynamic Array: Grow and Keep Contents

SystemVerilog Arrays

Grow a dynamic array with new[n](array) and add one more element

Open in IDE
testbench.sv
module tb;
  int scores [];

  initial begin
    scores = new [3];
    scores = '{10, 20, 30};

    scores = new [scores.size() + 1] (scores);
    scores [scores.size() - 1] = 40;

    $display ("scores = %p, size = %0d", scores, scores.size());
  end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.