【问题标题】:Are SystemVerilog arrays passed by value or reference?SystemVerilog 数组是按值传递还是按引用传递?
【发布时间】:2014-04-30 18:53:45
【问题描述】:

默认情况下,SystemVerilog 是按值还是按引用传递数组?

例如:

int array[5] = '{0,1,2,3,4};
some_function(array);  // <-- value or reference?

【问题讨论】:

    标签: arrays system-verilog


    【解决方案1】:

    默认情况下,SystemVerilog 按值传递数组,复制整个数组。

    出于性能原因,建议尽可能通过引用传递数组。

    • 如果您希望函数修改数组,请使用ref
    • 如果您希望函数读取数组,请使用const ref

    例子:

      function void pass_by_value(int array[5], int queue[$], int assoc[int]);
        // Default.
        // A copy of the arrays is made in this function
      endfunction
    
      function void pass_by_ref(ref int array[5], ref int queue[$],
                                ref int assoc[int]);
        // Original arrays are being referenced
      endfunction
    
      function void pass_by_const_ref(const ref int array[5],
                                      const ref int queue[$],
                                      const ref int assoc[int]);
        // Original arrays are being referenced
        // And they can be read but cannot be modified in this function 
      endfunction
    

    EDA Playground 示例:http://www.edaplayground.com/x/2m9

    【讨论】:

      猜你喜欢
      • 2016-11-28
      • 2017-11-14
      相关资源
      最近更新 更多