Difference Between Associative and Dynamic Array

Hi All,

Can any body tell me the

Difference between Associative and Dynamic Array.

Regards

Kunal Mishra

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.

Regards,
-Kurt

In reply to kurts:

Hi Kurt,

Can you through some more light on these concepts with respect to some real time scenario’s in our UVM environment.
As far as I have seen I have used associative array in some scoreboard logic where I will take a copy of the packet and store it in associative memory. Can you tell me any such scenario’s or show with some snipets of the code which explains the need of these arrays in the environmet.
Thanks in advance.