Under which circumstances MATLAB Coder may create statically allocated variables in the generated code?

조회 수: 1 (최근 30일)
It is sought to generate code using MATLAB Coder that does not generate statically allocated data. It is mentioned in the documentation page,
the following:
"If you do not specify reentrant code, MATLAB® Coder™ generates code that uses statically allocated memory for:
  • Function variables that are too large to fit on the stack
  • Global variables
  • Persistent variables"
However, when generating code from a custom MATLAB code that does not meet the latter requirements, the generated C/C++ code still contains statically allocated data.
Under which circumstances MATLAB Coder may create statically allocated variables in the generated code?

채택된 답변

MathWorks Support Team
MathWorks Support Team 2021년 5월 11일
The expected behavior is that MATLAB Coder generates statically allocated data for the scenarios that are mentioned the documentation page mentioned above, namely:
  • in the presence of persistent variables,
  • in the presence global variables,
  • in the presence of variables which would produce code that exceeds the 'StackUsageMax' configuration option.
However, it is in general difficult to predict whether a given piece of user-written MATLAB code may result in a C/C++ code that contains statically allocated data. The reason behind is that the code may transitively call MathWorks written MATLAB code which might eventually use persistent, global or large variables. Thus, the latter scenario cannot be excluded. Therefore, even if the user-written MATLAB Code does not use any of the latter three constructs, it is possible that it calls a MathWorks written MATLAB code that does.
The only certain way to ensure that no statically allocated data is generated is to enable the configuration option 'MultiInstanceCode' in the MATLAB Coder configuration options. Please visit the following MATLAB Answers that discusses this topic,
 
Without enabling the option 'MultiInstanceCode' in MATLAB Coder, it is possible to minimize the number of statically allocated variables by increasing the value of the configuration option 'StackUsageMax' supplied to codegen as described in the following documentation link,
The larger this value is, the less likely that local variables will be classified as too large for the stack. However, increasing this variable too much could result in code that allocates too much data on the stack, causing a stack overflow.

추가 답변 (0개)

태그

아직 태그를 입력하지 않았습니다.

제품


릴리스

R2014b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by