Control Dynamic Memory Allocation for Fixed-Size Arrays
The code generated by MATLAB®
Coder™ allocates memory to the program stack for fixed-size arrays whose size is
less than a specified threshold. For example, in the following code,
Z
is a fixed-size 1-by-4
array.
function Z = myfcn() Z = zeros(1,4); end
Dynamic memory allocation allocates memory on the heap, instead of the program stack. Consider dynamic memory allocation for large fixed-size arrays that might exhaust stack memory.
Dynamic memory allocation might result in slower execution of the generated code.
Enable Dynamic Memory Allocation for Fixed-Size Arrays
By default, dynamic memory allocation for fixed-size arrays is disabled. Use one of these methods to enable it.
In a configuration object for code generation, set the
EnableDynamicMemoryAllocation
andDynamicMemoryAllocationForFixedSizeArrays
properties totrue
.In MATLAB Coder, in the Memory settings, select Enable dynamic memory allocation and Enable dynamic memory allocation for fixed-sized arrays.
Set Dynamic Memory Allocation Threshold for Fixed-Size Arrays
When dynamic memory allocation for fixed-size arrays is enabled, the code generator allocates memory dynamically on the heap for fixed-size arrays whose size (in bytes) is greater than or equal to the dynamic memory allocation threshold.
The default dynamic memory allocation threshold is 64 kilobytes. To configure the threshold, use one of these:
In a configuration object for code generation, set a value for the property
DynamicMemoryAllocationThreshold
.In MATLAB Coder, in the Memory settings, set a value for Dynamic memory allocation threshold.
Generate Code for Fixed-Size Arrays
Define a MATLAB function that calculates the product of two large fixed-size
arrays a
and
b
.
function y = productLargeSize(a,b) y = a*b; end
cfg = coder.config('lib'); cfg.VerificationMode="SIL"; cfg.DynamicMemoryAllocationForFixedSizeArrays = true; t = coder.typeof(0,[1e4 1e4]); codegen productLargeSize -args {t,t} -config cfg - report
By enabling dynamic memory allocation for fixed-size arrays, arrays
a
and b
is dynamically allocated on the
heap, preventing stack overflow.
Generated Code
... void tlargeSize(const emxArray_real_T *a, const emxArray_real_T *b, emxArray_real_T *y) { const double *a_data; const double *b_data; double *y_data; ...
Usage Notes and Limitations
When dynamic memory allocation for fixed-size arrays is enabled:
Input and output variables of the function in the generated code are:
coder::array
variables for C++ code generation. See Use Dynamically Allocated C++ Arrays in Generated Function Interfaces.emxArray
structures for C code generation. See Use C Arrays in the Generated Function Interfaces
C/C++ function signatures must be updated when passing
struct
objects with fixed-size arrays fields tocoder.ceval
or Code Replacement Library (CRL).
Note
Enabling dynamic memory allocation for fixed-size arrays is not supported for GPU code generation.
See Also
coder.EmbeddedCodeConfig
| coder.MexCodeConfig
| coder.CodeConfig