Main Content

Control Memory Allocation for Fixed-Size Arrays

Dynamic memory allocation for fixed-size arrays allocates memory for the array on the heap instead of allocating memory on the program stack. Consider using dynamic memory allocation when:

  • The fixed-size arrays are large and you do not want to allocate memory on the stack.

  • Your target hardware memory is limited and you do not want to allocate memory for the arrays on the program stack.

For larger arrays, you can significantly reduce storage requirements. Dynamic memory allocation might result in slower execution of the generated code.

Enable Dynamic Memory Allocation for All 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 the DynamicMemoryAllocationForFixedSizeArrays parameter to 'Always'.

  • Alternatively, in the app, under Memory settings, set Dynamic memory allocation fixed-sized arrays to 'Always'.

The code generator dynamically allocates memory on the heap for all fixed-size arrays whose size is greater than 64 bytes.

Enable Dynamic Memory Allocation for Arrays Bigger Than a Threshold

Instead of allocating all fixed-size arrays dynamically on the heap, you can specify the threshold size above which memory is dynamically allocated. To instruct the code generator to use dynamic memory allocation for fixed-size arrays whose size is greater than or equal to the threshold:

  • In the configuration object, set the DynamicMemoryAllocationForFixedSizeArrays to 'Threshold'.

  • In the MATLAB Coder app, in the Memory settings, set Dynamic memory allocation for fixed-sized arrays to For arrays with max size at or above threshold.

The default dynamic memory allocation threshold is 64 kilobytes. To change the threshold:

  • In a configuration object for code generation, set the DynamicMemoryAllocationThreshold.

  • In the MATLAB Coder app, in the Memory settings, set Dynamic memory allocation threshold.

See Also

| |

Related Topics