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 threshold. For example, in the following
code, the array Z
is a fixed-size array with a first dimension of
size 1 and a second dimension of size
4.
function Z = myfcn() Z = zeros(1,4); end
Dynamic memory allocation allocates memory on the heap for fixed-size arrays, instead of the program stack. Consider dynamically allocating fixed-size arrays when you have large arrays that could 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. To enable it:
In a configuration object for code generation, set
EnableDynamicMemoryAllocation
andDynamicMemoryAllocationForFixedSizeArrays
options totrue
.In the MATLAB® Coder™ app, in the Memory settings, select Enable dynamic memory allocation and Enable dynamic memory allocation for fixed-sized arrays.
Dynamic Memory Allocation Threshold for Fixed-Size Arrays
When DynamicMemoryAllocationForFixedSizeArrays
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
DynamicMemoryAllocationThreshold
.
The default dynamic memory allocation threshold is 64 kilobytes. To configure the threshold:
In a configuration object for code generation, set a value for the
DynamicMemoryAllocationThreshold
.In the MATLAB Coder app, in the Memory settings, set a value for Dynamic memory allocation threshold.
Generating Code for Fixed-Size Arrays
Consider the following MATLAB function which calculates the product of two large
fixed-size arrays a
and
b
.
function y = tlargeSize(a,b) y = a*b; end
cfg = coder.config('lib'); cfg.VerificationMode="SIL"; cfg.DynamicMemoryAllocationForFixedSizeArrays = true; t=coder.typeof(0,[1e4 1e4]); codegen tlargeSize -args {t,t} -config cfg - report
By enabling the DynamicMemoryAllocationForFixedSizeArrays
option, arrays a
and b
are 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 enabling DynamicMemoryAllocationForFixedSizeArrays
:
Input and output variables of the function changes to:
coder::array
for C++ code generation. See, Use Dynamically Allocated C++ Arrays in Generated Function Interfaces.emxArray
for C code generation. See, Use C Arrays in the Generated Function Interfaces
Ensure C/C++ function signatures are updated when passing struct with fixed-size arrays to
coder.ceval
or Code Replacement Library (CRL).
Note
Enabling DynamicMemoryAllocationForFixedSizeArrays
is
not supported for GPU code generation.
See Also
coder.EmbeddedCodeConfig
| coder.MexCodeConfig
| coder.CodeConfig