Sunday, 29 March 2015

Difference between Dynamic Array and Assosicate Array in SystemVerilog

With a regular array, you must specify its size when you declare it

bit my_array[10];
With a dynamic array you can allocate the size of the array during runtime (hence the term "dynamic").
bit my_dynamic_array[];  // Note size is not specified
my_dynamic_array = new[size];  // size is determined at run-time

Also, the array can be "re-sized" at a later point:
my_dynamic_array = new[new_size](my_dynamic_array);

In this case, new memory is allocated, and the old array values are copied into the new memory, giving the effect of resizing the array.

The main characteristic of an associative array is that the index type can be any type - you are not restricted to just integer values. For example, you can use a string as the index to look up an "associated" value.

bit my_assoc_array[string];  // Array stores bits, using a string as an index
my_assoc_array["testname"] = 1;  //Stores a 1 into the array element indexed by the value "testname"
$display(my_assoc_array["testname"]); // Displays 1

An associative array is also "dynamic", in the sense that it does not have a pre-determined size. However, you do not have to allocate the size - it grows as you add more elements into it.

Another answer is:

    Dynamic arrays are useful for dealing with contiguous collections of variables whose number changes dynamically.
    e.g.            int array[];
    When the size of the collection is unknown or the data space is sparse (scattered-throw in various random directions.), an associative array is a better option. In associative array, it uses the transaction names as the keys in associative array.
   e.g.            int array[string];

No comments:

Post a Comment