Main Content

coder.areUnboundedVariableSizedArraysSupported

Check if current configuration settings allow unbounded variable-size arrays

Since R2024a

Description

example

tf = coder.areUnboundedVariableSizedArraysSupported returns a logical value that indicates whether unbounded variable-size arrays are supported during code generation or Simulink® model simulation.

  • During code generation from MATLAB® code, this function checks the states of code configuration settings Enable dynamic memory allocation (EnableDynamicMemoryAllocation) and Enable variable-sizing (EnableVariableSizing). If both settings are enabled, the function returns true. Otherwise, the function returns false.

  • During Simulink model simulation, this function returns the state of the configuration parameter Dynamic memory allocation in MATLAB functions (MATLABDynamicMemAlloc).

  • During code generation from Simulink models, this function checks the states of the configuration parameters Dynamic memory allocation in MATLAB functions (MATLABDynamicMemAlloc) and Support: variable-size signals (SupportVariableSizeSignals). If both of these parameters are enabled, the function returns true. Otherwise, it returns false.

  • In MATLAB execution, this function always returns true.

Examples

collapse all

Generate code for a MATLAB function sumrand that accepts a scalar input n and returns the sum of n random numbers. If unbounded variable-size arrays are supported, sumrand simply calls sum(rand(1,n)). Otherwise, it implements an algorithm that performs the computation without creating unbounded variable-size arrays.

Define the entry-point function sumrand that accepts a scalar input n and returns the sum of n random numbers.

function s = sumrand(n)
% Return the sum of n random numbers.
if coder.areUnboundedVariableSizedArraysSupported
    s = sum(rand(1,n));
else
    s = 0;
    blockSize = 128;
    nBlocks = floor(n/blockSize);
    % Add in the complete blocks.
    for k = 1:nBlocks
        s = s + sum(rand(1,blockSize));
    end
    % Process any partial block remaining.
    s = s + sum(rand(1,rem(n,blockSize)));
end

Generate code with the default configuration settings. The default settings support unbounded variable-size arrays. Therefore the code generator produces code for the if branch in the function sumrand.

codegen -config:lib -c sumrand -args 0 -d codegen_unbounded -report
Code generation successful: View report

Open the report and inspect the generated code. The generated C entry-point function sumrand uses the emxArray data structure to represent the unbounded variable-size array x.

double sumrand(double n)
{
  emxArray_real_T *x;
  double s;
  double *x_data;
  int ib;
  int k;
  if (!isInitialized_sumrand) {
    sumrand_initialize();
  }
  emxInit_real_T(&x);
  b_rand(n, x);
  x_data = x->data;
  if (x->size[1] == 0) {
    s = 0.0;
  } else {
    int firstBlockLength;
    int lastBlockLength;
    int nblocks;
    if (x->size[1] <= 1024) {
      firstBlockLength = x->size[1];
      lastBlockLength = 0;
      nblocks = 1;
    } else {
      firstBlockLength = 1024;
      nblocks = (int)((unsigned int)x->size[1] >> 10);
      lastBlockLength = x->size[1] - (nblocks << 10);
      if (lastBlockLength > 0) {
        nblocks++;
      } else {
        lastBlockLength = 1024;
      }
    }
    s = x_data[0];
    for (k = 2; k <= firstBlockLength; k++) {
      s += x_data[k - 1];
    }
    for (ib = 2; ib <= nblocks; ib++) {
      double bsum;
      int hi;
      firstBlockLength = (ib - 1) << 10;
      bsum = x_data[firstBlockLength];
      if (ib == nblocks) {
        hi = lastBlockLength;
      } else {
        hi = 1024;
      }
      for (k = 2; k <= hi; k++) {
        bsum += x_data[(firstBlockLength + k) - 1];
      }
      s += bsum;
    }
  }
  emxFree_real_T(&x);
  return s;
}

Generate code with dynamic memory allocation disabled. The modified configuration settings do not support unbounded variable-size arrays. Therefore, the code generator produces code for the else branch in the function sumrand.

cfg = coder.config('lib');
cfg.EnableDynamicMemoryAllocation = false;
codegen -config cfg -c sumrand -args 0 -d codegen_bounded -report

Open the report. Notice that the generated C entry-point function sumrand does not use the emxArray data structure and, therefore, does not use variable-size arrays.

double sumrand(double n)
{
  double x[128];
  double s;
  double y;
  int x_size[2];
  int b_k;
  int k;
  int vlen;
  if (!isInitialized_sumrand) {
    sumrand_initialize();
  }
  s = 0.0;
  /*  Add in the complete blocks. */
  vlen = (int)floor(n / 128.0);
  for (k = 0; k < vlen; k++) {
    b_rand(x);
    y = x[0];
    for (b_k = 0; b_k < 127; b_k++) {
      y += x[b_k + 1];
    }
    s += y;
  }
  /*  Process any partial block remaining. */
  c_rand(rt_remd_snf(n, 128.0), x, x_size);
  vlen = x_size[1];
  if (x_size[1] == 0) {
    y = 0.0;
  } else {
    y = x[0];
    for (k = 2; k <= vlen; k++) {
      y += x[k - 1];
    }
  }
  s += y;
  return s;
}

Version History

Introduced in R2024a