# How to determine the size of a multi-dimensional dynamic array in a constraint?

**URL:** <https://verificationacademy.com/forums/t/how-to-determine-the-size-of-a-multi-dimensional-dynamic-array-in-a-constraint/31948>\
**Category:** SystemVerilog\
**Tags:** multidimensional-array-systemverilog, SystemVerilog\
**Created:** [June 15, 2016, 12:32am UTC](https://verificationacademy.com/forums/t/how-to-determine-the-size-of-a-multi-dimensional-dynamic-array-in-a-constraint/31948 "2016-06-15T00:32:52Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![dshanley](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/f1d935/32.png) [@dshanley](https://verificationacademy.com/forums/u/dshanley)\
**Post date:** [June 15, 2016, 12:04pm UTC](https://verificationacademy.com/forums/t/how-to-determine-the-size-of-a-multi-dimensional-dynamic-array-in-a-constraint/31948/3 "2016-06-15T12:04:15Z")

</div>

_In reply to [prashant.kaushik](https://verificationacademy.com/forums/t/how-to-determine-the-size-of-a-multi-dimensional-dynamic-array-in-a-constraint/31948/2):_

Hi,

Thanks for your help, but it still doesn’t work for me.

The total\_size function always returns 0 and the solver fails.

The “solve sgl before payload\_desc;” doesn’t appear to have any effect.

If I hack the total\_size function to return, say 3. Then the solver passes as expected.

```verilog
module top;

  typedef byte sgl_t[][][16];

  // Class
  class myclass;
    rand byte sgl[][][16];

    rand byte unsigned payload[];
    rand int unsigned payload_desc[];

    // Array creation
    constraint c_sgl_size { sgl.size == 5;
                             payload.size == 15;

                             foreach(sgl[i,,]) {
                               sgl[i].size == 2;
                             }

                           }

    function int total_size(sgl_t x);
           foreach (x[i,]) begin
               total_size += x[i].size();
           end
           return total_size;
           //return 3;
    endfunction

    constraint C_PAYLOAD_DISTRIBUTION {
        solve sgl before payload_desc;
        payload_desc.size == total_size(sgl);
        payload_desc.sum with (longint'(item)) == payload.size;
    }

    function void post_randomize();

      $display("payload_desc size = %d ", payload_desc.size());

      $display("sgl size = %d ", sgl.size());
      foreach(sgl[i,]) begin
        $display("sgl[%1d].size= %d ", i, sgl[i].size);
      end

      foreach(payload_desc[i]) begin
        $display("payload_desc[%1d]= %d ", i, payload_desc[i]);
      end
    endfunction: post_randomize

  endclass // myclass

  initial
  begin
    myclass c;

    c= new();
    if(!c.randomize()) begin
      $display("Randomize fail ");
    end
    else begin
      $display("Randomize Pass");
    end
  end
endmodule // top;

```

---

_[View the full topic](https://verificationacademy.com/forums/t/how-to-determine-the-size-of-a-multi-dimensional-dynamic-array-in-a-constraint/31948)._
