# Question on associative arrays

**URL:** <https://verificationacademy.com/forums/t/question-on-associative-arrays/46445>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, associative-array\
**Created:** [March 10, 2024, 10:39pm UTC](https://verificationacademy.com/forums/t/question-on-associative-arrays/46445 "2024-03-10T22:39:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kranthikiranufl1](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/f4b2a3/32.png) [@kranthikiranufl1](https://verificationacademy.com/forums/u/kranthikiranufl1)\
**Post date:** [March 10, 2024, 10:39pm UTC](https://verificationacademy.com/forums/t/question-on-associative-arrays/46445/1 "2024-03-10T22:39:00Z")

</div>

I have the following code. I was trying to create a unique 3x3x3 matrix with unique elements. After randomization I am storing them into an associative array and later printing the elements of the associative array. I was expecting the size of associative array is 27. But I am seeing the size as 32.

Could some one explain this behavior of associative array?

```auto
class matrix;
 
  rand int a [3][3][3];
  rand int b [3][3][3];
  int A[int] = '{default:0};
 
  constraint c1 {
    foreach(b[i,j,k])
      b[i][j][k] inside {[1:35]};
    unique{b};
  }

  function void post_randomize();
    $display("%p", a);
    foreach(b[i,j,k]) begin
      $display("%d", b[i][j][k]);
      A[b[i][j][k]]=1;
    end
    foreach(A[i])
      $display("A[%d]= %d", i, A[i]);
    
    $display("A.size:%d", A.size);
  endfunction

endclass

module tb;
  
  matrix C;

  initial begin
      C = new;
    repeat(5)
      C.randomize();
    
  end
  
endmodule

```

---

<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:** [March 10, 2024, 11:16pm UTC](https://verificationacademy.com/forums/t/question-on-associative-arrays/46445/2 "2024-03-10T23:16:20Z")

</div>

The size of `A` is 27 after the first time you call randomize. You would need to call `A.delete()` to erase the previous contents of the array.
