This example shows how to specify a dynamic memory allocation threshold for variable-size arrays. Dynamic memory allocation optimizes storage requirements for variable-size arrays, but causes slower execution of generated code. Instead of disabling dynamic memory allocation for all variable-size arrays, you can disable dynamic memory allocation for arrays less than a certain size.
Specify this threshold when you want to:
Disable dynamic memory allocation for smaller arrays. For smaller arrays, static memory allocation can speed up generated code. Static memory allocation can lead to unused storage space. However, you can decide that the unused storage space is not a significant consideration for smaller arrays.
Enable dynamic memory allocation for larger arrays. For larger arrays, when you use dynamic memory allocation, you can significantly reduce storage requirements.
To open the Generate dialog box, on the
Generate Code page, click the
Generate arrow .
Click More Settings.
On the Memory tab, select the Enable variable-sizing check box.
Set Dynamic memory allocation to For
arrays with max size at or above threshold
.
Set Dynamic memory allocation threshold to the value that you want.
The Dynamic memory allocation threshold value
is measured in bytes. Based on information from the target hardware
settings, the software estimates the size of the array that a certain
value of DynamicMemoryAllocationThreshold
can accommodate.
This estimate excludes possible C compiler optimizations such as putting
variables in registers.
Create a configuration object for code generation.
Use coder.config
with arguments 'lib'
,'dll'
,
or 'exe'
(depending on your requirements). For
example:
cfg = coder.config('lib');
Set DynamicMemoryAllocation
to 'Threshold'
.
cfg.DynamicMemoryAllocation='Threshold';
Set the property, DynamicMemoryAllocationThreshold
,
to the value that you want.
cfg.DynamicMemoryAllocationThreshold = 40000;
The value stored in DynamicMemoryAllocationThreshold
is
measured in bytes. Based on information from the target hardware settings,
the software estimates the size of the array that a certain value
of DynamicMemoryAllocationThreshold
can accommodate.
This estimate excludes possible C compiler optimizations such as putting
variables in registers.