Main Content

Customize Stack Space Allocation

Your application might be constrained by limited memory. Controlling the maximum allowable size for the stack is one way to modify whether data is defined as local or global in the generated code. You can limit the use of stack space by specifying a positive integer value for the Maximum stack size (bytes) parameter, on the Optimization pane of the Configuration parameter dialog box. Specifying the maximum allowable stack size provides control over the number of local and global variables in the generated code. Specifically, lowering the maximum stack size might generate more variables into global structures. The number of local and global variables help determine the required amount of stack space for execution of the generated code.

The default setting for Maximum stack size (bytes) is Inherit from target. In this case, the value of the maximum stack size is the smaller value of the following: the default value set by the code generator (200,000 bytes) or the value of the TLC variable MaxStackSize found in the system target file (ert.tlc).

To specify a smaller stack size for your application, select the Specify a value option of the Maximum stack size (bytes) parameter and enter a positive integer value. To specify a smaller stack size at the command line, use:

set_param(model_name, 'MaxStackSize', 65000);

Note

For overall executable stack usage metrics, you might want to do a target-specific measurement, such as using runtime (empirical) analysis or static (code path) analysis with object code.

It is recommended that you use the Maximum stack size (bytes) parameter to control stack space allocation instead of modifying the TLC variable, MaxStackSize, in the system target file. However, a target author might want to set the TLC variable, MaxStackSize, for a target. To set MaxStackSize, use assign statements in the system target file (ert.tlc), as in the following example.

%assign MaxStackSize = 4096

Write your %assign statements in the Configure RTW code generation settings section of the system target file. The %assign statement is described in Target Language Compiler.

See Also

Related Topics