Question

I am having a class packet with a dynamic array. I would like to know if the new / randomize function of the class object can allocate memory for the dynamic array.

class packet;
rand int data[];
constraint c_data_size { data.size == 2; };
endclass : packet

program test;
packet pk1;
pk1 = new();
$display(" data.size = %d", data.size);
if(pk1.randomize) begin
$display(" data.size = %d", data.size);
data[0] = 23;
data[1] = 23432;
end

endprogram

Output
data.size = 0
data.size = 2

From this example code, I understand that new() function does not allocate memory for dynamic array data[] but can randomize allocate memory for data[] as I have not called "data = new[2];" on dynamic array. Thanks for your time.

Was it helpful?

Solution

It's like you already said, new() doesn't allocate memory. Calling randomize() does allocate memory on dynamic arrays; how much depends on the constraints.

OTHER TIPS

It is part of LRM. See IEEE Std 1800-2012 § 18.4 Random variables

  • ...
  • 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 array size constraint is declared using the size method.For example:
    
    rand bit [7:0] len;
    rand integer data[];
    constraint db { data.size == len; }
    The variable len is declared to be 8 bits wide. The randomizer computes a random value for the len variable in the 8-bit range of 0 to 255 and then randomizes the first len elements of the data array. When a dynamic array is resized by randomize, the resized array is initialized (see 7.5.1) with the original array. ...
    If a dynamic array’s size is not constrained, then the array shall not be resized and all the array elements shall be randomized.
  • ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top