Size constraint on array and queue

Hi,

The size() constraint which can be applied on array.
Is it correct/possible to do it on a queue as well?


class Object;
   rand bit[7:0] array[];
   rand bit[7:0] queue[$];
   constraint queue_val {
     array.size() inside {[10:20]}; 
     queue.size() inside {[10:20]}; 
   }
endclass

Are there any gotchas regards it?

Thanks!

In reply to Michael54:

The size of a dynamic array or queue declared as rand or randc can also be constrained. In that case, the array shall be resized according to the size constraint, and then all the array elements shall be randomized.

The only gotcha with resizing any kind of array is when you have an array of class handles. Randomization will not construct new class objects if the size is lengthened.

In reply to dave_59:

Thank you Dave for the quick replay :)

Will the size constraint work the same also for array/queue of structs as described in the next example?
Should not we use the new() function for allocation of the struct items? or is it done automatically?


class packet;
    typedef struct {
       int  addr;
       byte data [];
    } st_item;
    
    rand st_item array[];
    rand st_item queue[$];
    
    constraint sizes_c {
      array.size() inside {[10:20]}; 
      queue.size() inside {[10:20]}; 
    }
endclass;

Next part of this thread will be the same for array/queue of classes… if the size constraint will work these as well? And if any special treatment needed in the pre_randomize or something else?

Thanks!