Hi,
I have an associative array which is defined inside a function, but when the function is finished, the associative array is not cleared. When I recall the function, the associative array still have the item from last function call.
Here is the code:
class A;
rand int num[10];
rand int helper[10];
constraint num_c{
foreach(num[i]){
num[i] inside {[0:9]};
}
}
endclass
module top;
initial begin
A a = new();
repeat(10) begin
a.randomize();
$display("randomized number: %0p", a.num);
check_un(a.num);
end
end
endmodule
// check the number of unqiue number in a array
function int check_un(int arr[]);
int hash_arr[int];
int unique_arr[$];
int result;
for(int i=0;i<arr.size();i++) begin
if(hash_arr.exists(arr[i])) hash_arr[arr[i]]++;
else hash_arr[arr[i]] = 1;
end
unique_arr = hash_arr.find_index() with (item == 1);
result = unique_arr.size();
$display("hash arr is %p", hash_arr);
return result;
endfunction
Here is the output:
randomized number: '{0, 1, 4, 4, 4, 6, 7, 7, 7, 7}
hash arr is '{0x0:1, 0x1:1, 0x4:3, 0x6:1, 0x7:4}
randomized number: '{1, 9, 3, 9, 2, 9, 2, 1, 9, 2}
hash arr is '{0x0:1, 0x1:3, 0x2:3, 0x3:1, 0x4:3, 0x6:1, 0x7:4, 0x9:4}
randomized number: '{1, 3, 3, 8, 6, 8, 2, 5, 7, 9}
hash arr is '{0x0:1, 0x1:4, 0x2:4, 0x3:3, 0x4:3, 0x5:1, 0x6:2, 0x7:5, 0x8:2, 0x9:5}
edaplayground link: https://edaplayground.com/x/Etz3