Use Generated Initialize and Terminate Functions - MATLAB & Simulink (original) (raw)

When generating C/C++ code from MATLAB® code, the code generator automatically produces two housekeeping functions, initialize and terminate. The initialize function initializes the state on which the generated C/C++ entry-point functions operate. It must be called before you call the entry-point functions for the first time. The terminate function frees allocated memory and performs other cleanup operations. It must be called after you call the entry-point functions for the last time.

Initialize Function

The name of the generated initialize function is_primary_function_name__initialize, where_primary_function_name_ is the name of the first MATLAB entry-point function that you specify while generating code. The initialize function initializes the state on which the generated entry-point functions operate. The initialize function can include:

In certain situations, no initialization code is necessary and the generated initialize function is empty.

Calling Initialize Functions

If you generate a MEX function, the generated code automatically includes a call to the initialize function. If you generate standalone code, there are two possible situations:

If you generate C++ code with a class interface, then the code generator produces a class constructor and destructor that perform initialization and termination operations. You do not need to manually call the initialize andterminate functions. See Generate C++ Code with Class Interface.

Examples of Generated Initialize Functions

Examples of MATLAB code patterns and the corresponding generated initialize functions:

Terminate Function

The name of the generated terminate function is_primary_function_name__terminate, where_primary_function_name_ is the name of the first MATLAB entry-point function that you specify while generating code. The terminate function frees allocated memory and performs other cleanup operations.

The terminate function can also include custom cleanup code that you specify. To include custom code in the terminate function, do one of the following:

If you generate a MEX function, the generated code automatically includes a call to the terminate function.

If you generate standalone code, the generated code does not automatically include a call to the terminate function. In this situation, you must manually invoke the terminate function after you call the generated entry-point functions for the last time.

Terminate functions are also used to clear the state of persistent variables. A persistent variable retains its state until a terminate function is invoked. For more information, see Generate Code for Persistent Variables.

Example of Generated Terminate Function

Define this MATLAB function:

function y = bar global g y = g; end

Generate a static library for bar. Specify the initial value ofg as1.

codegen -config:lib -globals {'g',1} bar

The code generator produces the file bar_terminate.c in_`work`_\codegen\lib\bar, where_`work`_ is the folder that containsbar.m. The function bar_terminate sets the boolean isInitialized_bar (that was set to true after the initialize function call) tofalse.

void bar_terminate(void) { isInitialized_bar = false; }

See Also

coder.MexCodeConfig | coder.CodeConfig | coder.EmbeddedCodeConfig