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

Class Handle: Dynamic Array

SystemVerilog Classes & OOP

Runnable example from the SystemVerilog Class Handle tutorial (Dynamic Array)

Open in IDE
design.sv
class Packet;
    int count;
    function new (int c = 0);
        count = c;
    endfunction
endclass
testbench.sv
module tb;

    initial begin
        // Declare a dynamic array of Packet objects
        Packet dynamic_array[];

        $display("--- Dynamic Array Example ---");

        // Initialize the dynamic array with a specific size
        dynamic_array = new[3]; // Creates space for 3 Packet references

        // Initialize each element of the dynamic array
        for (int i = 0; i < dynamic_array.size(); i++) begin
            dynamic_array[i] = new(i + 1); // Create new Packet objects
        end

        // Access and display data
        foreach (dynamic_array[i]) begin
            $display("dynamic_array[%0d].count = %0d", i, dynamic_array[i].count);
        end
    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.