Skip to content
asicguru.tech@gmail.com
REGISTER LOGIN
AsicGuru Ventures
  • All Courses
  • CONTACT
  • GALLERY
  • HOME
  • Tutorials

0

Verilog Tutorial

17
  • Introduction
  • Lexical Tokens
  • Gate-Level Modelling
  • Data Types
  • Operators
  • Operands
  • Modules
  • Behavioral Modeling
  • Timing Controls
  • Procedures: Always and Initial Blocks
  • Functions
  • Tasks
  • Component Inference
  • Finite State Machines.
  • Compiler Directives
  • System Tasks and Functions
  • Test Benches

System Verilog

23
  • System Verilog Introduction
  • Data Types
  • Type Casting
  • Arrays
  • Associative Arrays
  • Dynamic Arrays
  • Queue
  • Operators
  • Procedural statements and Control flow
  • Classes
  • Inheritance
  • Encapsulation
  • This and Super operator
  • In-Line Constraints
  • Class Constraints
  • Virtual Classes
  • Parameterized Classes
  • Classes Summary
  • Singleton Classes
  • Functional Coverage Introduction
  • Covergroup
  • Tools
  • Books

Scripting

15
  • Introduction
  • Perl Tutorial
  • What is Perl
  • Perl: Syntax And Variable
  • Perl Strings
  • Perl Arrays
  • Perl Associative Arrays
  • If/While Syntax
  • File Input
  • Print Output
  • String Processing with Regular Expressions
  • Subroutines
  • Running External Programs
  • References
  • Terse Perl

Makefile

1
  • Makefile Tutorial

Interview Questions

2
  • SystemVerilog Interview Questions
  • Verilog Interview Questions

Handwritten Notes

3
  • Perl (Scripting)
  • Linting
  • CMOS Logic Design
View Categories
  • Home
  • Tutorials
  • System Verilog
  • Dynamic Arrays

Dynamic Arrays

1 min read

What is a Dynamic Array? #

A Dynamic Array is a one-dimensional unpacked array whose size can be set and changed during simulation runtime using the new[] operator.

Unlike fixed arrays, dynamic arrays are ideal when the number of elements is not known in advance, such as in packet-based verification scenarios where the size can vary with each transaction.

Syntax:

data_type array_name[];
array_name = new[size];         // Allocates memory for given size

Example: Declaration and Initialization

module example;
  int dyn_array[];               // Declare a dynamic array
  dyn_array = new[5];            // Allocate 5 elements

  foreach (dyn_array[i])
    dyn_array[i] = i * 10;       // Initialize values: 0, 10, 20, 30, 40
endmodule

Built-in Methods

MethodDescription
.size()Returns the number of elements
.delete()Frees memory and resets array to size 0

Resizing Dynamic Arrays

You can resize a dynamic array at runtime:

dyn_array = new[20];              // Resize with data loss
dyn_array = new[10](dyn_array);   // Resize and keep old values

If you don’t use (old_array) during resizing, previous values are discarded.

Deleting a Dynamic Array

dyn_array.delete();               // Clears all elements

After deletion:

  • Array size becomes 0
  • Memory is released

Example:

module test;
  int dyn_array[];

  initial begin
    dyn_array = new[4];             // Allocate 4 elements
    foreach (dyn_array[i])
      dyn_array[i] = i + 1;

    $display("Original size: %0d", dyn_array.size());  

    dyn_array = new[8](dyn_array);  // Resize and keep old values
    dyn_array[4] = 5;
    dyn_array[5] = 6;

    $display("Resized size: %0d", dyn_array.size());   

    dyn_array.delete();             // Free memory
    $display("After delete: %0d", dyn_array.size());   
  end
endmodule

Output:

Original size: 4
Resized size:  8
After delete:  0

Comparison: Dynamic vs Fixed Arrays

FeatureFixed ArrayDynamic Array
Size defined atCompile timeSimulation time (runtime)
Memory allocationStatic (stack)Dynamic (heap)
Resizable❌ No✅ Yes
Dimensions allowed1D / Multi-DOnly 1D
Out-of-bound accessReturns ‘x’Causes runtime error
Supports methods❌ No✅ Yes (size(), delete())
Updated on August 4, 2025

What are your Feelings

  • Happy
  • Normal
  • Sad
Share This Article :
  • Facebook
  • X
  • LinkedIn
  • Pinterest
Associative ArraysQueue

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Table of Contents
  • What is a Dynamic Array?

Archives

  • May 2025

Categories

  • Slider Post
  • Uncategorized

Copyright @ Asicguru Ventures by Misbah WP | Proudly powered by WordPress