# Queues of Dynamic Arrays

**URL:** <https://verificationacademy.com/forums/t/queues-of-dynamic-arrays/32864>\
**Category:** SystemVerilog\
**Tags:** dynamic-arrays, SystemVerilog, queue\
**Created:** [February 15, 2017, 7:49pm UTC](https://verificationacademy.com/forums/t/queues-of-dynamic-arrays/32864 "2017-02-15T19:49:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jin\_hsueh](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/5f9b8f/32.png) [@jin\_hsueh](https://verificationacademy.com/forums/u/jin_hsueh)\
**Post date:** [February 15, 2017, 7:49pm UTC](https://verificationacademy.com/forums/t/queues-of-dynamic-arrays/32864/1 "2017-02-15T19:49:31Z")

</div>

Hi,

Are queues of dynamic arrays allowed in either SystemVerilog or VMM? For example:

logic [7:0] Qda [$];

if that’s allowed, how do I initialize the dimension of the dynamic arrays at run-time? Would this still work:

Qda = new[10];

and lastly, suppose I have an dynamic array of the same size:

logic [7:0] DA;  
DA = new[10];

how would I push\_back this into Qda, essentially adding one more element to the queue? Thanks in advance!

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [February 15, 2017, 8:54pm UTC](https://verificationacademy.com/forums/t/queues-of-dynamic-arrays/32864/2 "2017-02-15T20:54:46Z")

</div>

_In reply to [jin\_hsueh](https://verificationacademy.com/forums/t/queues-of-dynamic-arrays/32864):_  
Yes you can have queues of dynamic arrays in SystemVerilog, but remember that you are declaring an array of an array, not really a multidimensional array. The difference is each dynamic array element in the queue can have a different dynamic array size.

```verilog
module top;
   logic [7:0] Qda[$] []; // this is a Queue -> of dynamic arrays -> of logic [7:0]
   logic [7:0] DA[]={1,2,3};
   initial begin
      Qda = '{10{ {} }}; // initializes a Queue of 10 empty dynamic arrays
      Qda.push_back(DA); // pushes the eleventh Queue element by copying the DA {1,2,3}
      $display("%p",Qda);
   end
endmodule

```

---

<div class="post-metadata">

**Author:** ![jin\_hsueh](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/5f9b8f/32.png) [@jin\_hsueh](https://verificationacademy.com/forums/u/jin_hsueh)\
**Post date:** [February 17, 2017, 10:59pm UTC](https://verificationacademy.com/forums/t/queues-of-dynamic-arrays/32864/3 "2017-02-17T22:59:34Z")

</div>

That works. Thank you very much!

---

<div class="post-metadata">

**Author:** ![jin\_hsueh](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/5f9b8f/32.png) [@jin\_hsueh](https://verificationacademy.com/forums/u/jin_hsueh)\
**Post date:** [February 21, 2017, 6:36pm UTC](https://verificationacademy.com/forums/t/queues-of-dynamic-arrays/32864/4 "2017-02-21T18:36:53Z")

</div>

_In reply to [jin17](https://verificationacademy.com/forums/t/queues-of-dynamic-arrays/32864/3):_

> That works. Thank you very much!

Hi Dave,

Now I’m running into an issue trying to assign a slice of the array above. Instead of

logic [7:0] DA;

I had:

logic [15:0] DA2;

But the following assignment would generate a compile error:

DA2[11:0] = Qda.pop\_front();

Error- Incompatible complex type  
…  
Incompatible complex type assignment  
Type of source expression is incompatible with type of target expression.  
Mismatching types cannot be used in assignments, initializations and  
instantiations. The type of the target is ‘logic[15:0][]', while the type of the source is 'logic[7:0]’.  
Source Expression: this.Qda.pop\_front

* * *

My intention is to pop the first dynamic array off Qda, and assign it to DA2. But since the packed dimensions are different, I want to put each byte of Qda’s lowest-indexed dynamic array into the LSB’s of each word of DA2. What’s the best way to accomplish this? Thanks again in advance.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [February 21, 2017, 7:10pm UTC](https://verificationacademy.com/forums/t/queues-of-dynamic-arrays/32864/5 "2017-02-21T19:10:46Z")

</div>

_In reply to [jin17](https://verificationacademy.com/forums/t/queues-of-dynamic-arrays/32864/4):_  
Assignments only work with contiguous sets of bits. You will need to pop into an intermediate dynamic array variable, and then use a foreach loop.

```verilog
module top;
   logic [7:0] Qda[$] []; // this is a Queue -> of dynamic arrays -> of logic [7:0]
   logic [7:0] DA[]={1,2,3};
   logic [7:0] temp_da[];
   logic [15:0] DA2[];
   initial begin
      Qda = '{10{ {} }}; // initializes a Queue of 10 empty dynamic arrays
      Qda.push_back(DA); // pushes the eleventh Queue element by copying the DA {1,2,3}
      //...
      temp_da = Qda.pop_front();
      DA2 = new[temp_da.size];
      foreach(DA2[ii]) DA2[ii] = temp_da[ii];
      $display("%p",DA2);
   end
endmodule

```

---

<div class="post-metadata">

**Author:** ![jin\_hsueh](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/5f9b8f/32.png) [@jin\_hsueh](https://verificationacademy.com/forums/u/jin_hsueh)\
**Post date:** [February 22, 2017, 6:03pm UTC](https://verificationacademy.com/forums/t/queues-of-dynamic-arrays/32864/6 "2017-02-22T18:03:50Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/queues-of-dynamic-arrays/32864/5):_

You are good. Thanks again!

---

<div class="post-metadata">

**Author:** ![VE1](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/54ee81/32.png) [@VE1](https://verificationacademy.com/forums/u/VE1)\
**Post date:** [July 8, 2018, 7:27pm UTC](https://verificationacademy.com/forums/t/queues-of-dynamic-arrays/32864/7 "2018-07-08T19:27:19Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/queues-of-dynamic-arrays/32864/6):_

Dave :  
I would like to confirm it  
logic [7:0] Qda[] []; this is a\*\*Queue -\> of dynamic arrays -\> of logic [7:0]\*\* ? logic [7:0] Qda[] [];  
this is a**dynamic array → of Q → of logic [7:0]**?

Thanks

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [July 9, 2018, 3:34pm UTC](https://verificationacademy.com/forums/t/queues-of-dynamic-arrays/32864/8 "2018-07-09T15:34:13Z")

</div>

_In reply to [VE](https://verificationacademy.com/forums/t/queues-of-dynamic-arrays/32864/7):_  
Try using   
.push\_back() to confirm.
