What is the difference between queue and associative array

Hi,

I have a doubt on queue and associative array.
For Example:
case:1

 class A extends base_sequence;
   sequence seq1[$];   --> Queue
   task body()
    ..
    for(i=0; i<15; i++) begin
    `uvm_do_with{seq1[i], ....
                }
   endtask
 endclass

case:2

class A extends base_sequence;
   sequence seq1[int]; -->Assoc Array
   task body()
    ..
    for(i=0; i<15; i++) begin
    `uvm_do_with{seq1[i], ....
                }
   endtask
 endclass

May i know which one of the above case is correct with respect to “`uvm_do_with{seq1[i], …” and why?

When i use queue in the above scenario, i got error like out_of_bound of the queue.

May i know what is the reason?

In reply to pkoti0583:

It helps to add code tags to your post. I have done that for you.

The key difference between a queue and an associative array is how individual elements are added or removed.

You have to explicitly “push” to allocate elements to the front or back of a queue, and the elements are always indexed with a value from 0 at the front to the size of the queue minus 1 at the back. Queues are lists with efficient access and modification to the front or back of the queue.

Elements of an associate array are allocated on the first write to an element with any index value. The index values do not need to be contiguous, which is why they are sometimes called sparse arrays.

When you know the number of elements of an array you will need and that will not change while using the array, then use a dynamic array and allocate the elements with the new[N] statement.

Since you are using the `uvm_do_with() macro, you do not get to see and understand the statements behind the macro:

 $cast(seq[i] , create_item(w_, m_sequencer, "seq[i]"));

The $cast statement is the first assignment to an array element. It will get created automatically for an associative array, but not for a queue. Also note that the name of the sequence will be “seq[i]” for all elements. The [i] does not get interpreted.

The Verification Academy strongly suggests that you do not use the `uvm_do macros and instead learn the statements needed to construct and start a sequence.
For a queue:

seq.push_back(sequence::type_id::create($sformatf("seq[%0d]")));

For an associative array or dynamic array:

seq[i] = sequence::type_id::create($sformatf("seq[%0d]")));