Main Content

Remove Zero-Initialization Code

The Remove root level I/O zero initialization and Remove internal data zero initialization parameters control whether the generated code contains initialization code for internal data (block states and block outputs) and external data (root inports and outports) whose value is zero. Eliminating zero-initialization code accelerates model initialization, reduces ROM consumption, and increases the execution speed of the generated code.

During startup, standards-compliant C and C++ compilers initialize global data to zero, eliminating the need to include zero-initialization code for this data in the generated code. Standards-compliant compilers do not necessarily initialize dynamically allocated data and local variables to zero. Before selecting the Remove root level I/O zero initialization and Remove internal data zero initialization parameters:

  • If your compiler is not standards-compliant, confirm that it initializes global data to zero.

  • If you set the Code interface packaging parameter to Reusable function or C++ class, confirm that data is either statically allocated or that the code initializes dynamically allocated data to zero.

If you set the Code interface packaging parameter to Reusable function and select the Use dynamic memory allocation for model initialization parameter:

  • The Remove root level I/O zero initialization and Remove internal data zero initialization check boxes are cleared.

  • At the command line, ZeroExternalMemoryAtStartup and ZeroInternalMemoryAtStartup parameters are set to 'on'.

If you set the Code interface packaging parameter to C++ class and select the Use dynamic memory allocation for model block instantiation parameter:

  • The Remove internal data zero initialization check box is cleared.

  • The ZeroInternalMemoryAtStartup parameter is set to 'on' and is read-only.

Remove Zero Initialization Code for Internal Data

This example shows how to eliminate code that initializes internal data to zero.

Example Model

Open the InternalZeroInitialization model. The model contains an enabled subsystem whose initial output is zero. The subsystem contains a Unit Delay block whose initial condition is 0.

model = 'InternalZeroInitialization';
open_system(model);

Generate Code Without Optimization

Build the model by using Embedded Coder®.

evalc('slbuild(model)');

This code is in the InternalZeroInitialization.c file.

cfile = fullfile('InternalZeroInitialization_ert_rtw','InternalZeroInitialization.c');
coder.example.extractLines(cfile,'/* Model initialize', '* File trailer', 1, 0);
/* Model initialize function */
void InternalZeroInitialization_initialize(void)
{
  /* Registration code */

  /* initialize error status */
  rtmSetErrorStatus(rtM, (NULL));

  /* states (dwork) */
  (void) memset((void *)&rtDWork, 0,
                sizeof(D_Work));

  /* SystemInitialize for Enabled SubSystem: '<Root>/Enabled Subsystem' */
  /* InitializeConditions for UnitDelay: '<S1>/Unit Delay' */
  rtDWork.UnitDelay_DSTATE = 0.0;

  /* End of SystemInitialize for SubSystem: '<Root>/Enabled Subsystem' */
}

/*

Enable Optimization

Open the Configuration Parameters dialog box. On the Optimization pane, select Remove internal data zero initialization.

Alternatively, you can use the command prompt to enable the optimization. Set the model parameter ZeroInternalMemoryAtStartup to 'off'.

set_param(model, 'ZeroInternalMemoryAtStartup', 'off');
set_param(model, 'ZeroInternalMemoryAtStartup', 'off');

Generate Code with Optimization

Build the model by using Embedded Coder.

evalc('slbuild(model)');

This code is in the InternalZeroInitialization.c file. The generated code does not initialize internal data by assignment to zero.

coder.example.extractLines(cfile,'/* Model initialize', '* File trailer', 1, 0);
/* Model initialize function */
void InternalZeroInitialization_initialize(void)
{
  /* (no initialization code required) */
}

/*
bdclose(model)

Remove Initialization Code from Root-Level Inports and Outports Set to Zero

This example shows how to remove initialization code from root-level inports and outports set to zero.

Example Model

In the RootZeroInitialization model, all of the input and output signals have a numeric value of zero. Because signals sig1 and sig2 have data types int16 and Boolean, respectively, and all of the output signals have data type double, these signals also have initial values of bitwise zero. The signals have an integer bit pattern of 0, meaning that all bits are off. Signals sig1_b and sig2_b have a fixed-point data type with bias, so their initial value is not bitwise zero.

model = 'RootZeroInitialization';
open_system(model);

Generate Code

% Build the model by using Embedded Coder.
set_param(model, 'ZeroExternalMemoryAtStartup','on');
evalc('slbuild(model)');

These lines of RootZeroInitialization.c code show the initialization of root-level inports and outports without the optimization. The four input signals are individually initialized as global variables. The four output signals are members of a global structure that the memset function initializes to bitwise zero.

cfile = fullfile('RootZeroInitialization_ert_rtw',...
    'RootZeroInitialization.c');
coder.example.extractLines(cfile, '/* Model initialize function */',...
    'trailer for generated code', 1, 0);
/* Model initialize function */
void initialize(void)
{
  /* Registration code */

  /* external inputs */
  sig1 = 0;
  sig2 = false;
  sig1_b = -3;
  sig2_b = -3;

  /* external outputs */
  (void)memset(&rtY, 0, sizeof(ExternalOutputs));
}

/*

Enable Optimization

  1. Open the Configuration Parameters dialog box.

  2. On the Optimization pane, select Remove root level I/O zero initialization.

Alternatively, use the command-line API to enable the optimization:

 set_param(model, 'ZeroExternalMemoryAtStartup','off');

Generate Code with Optimization

The optimized code does not contain initialization code for the input signals sig1, sig2, and the four output signals because their initial values are bitwise zero.

Build the model.

evalc('slbuild(model)');

Here is the RootZeroInitialization.c optimized code in the initialization function.

cfile = fullfile('RootZeroInitialization_ert_rtw',...
    'RootZeroInitialization.c');
coder.example.extractLines(cfile, '/* Model initialize function */',...
    'trailer for generated code', 1, 0);
/* Model initialize function */
void initialize(void)
{
  /* Registration code */

  /* external inputs */
  sig1_b = -3;
  sig2_b = -3;
}

/*

Close the model and the code generation report.

bdclose(model)

Additional Information

  • By default, the code generator does not generate initialization code for local variables set to zero. You can disable this optimization by clearing the Remove local variable initialization to zero value parameter. Clear this parameter to generate code that complies with coding standards such as MISRA C++:2008 Rule 0-1-4 (Polyspace Bug Finder). For more information, see Control Generation of Initialization Code for Local Variables Set to Zero.

  • You can use the Use memset to initialize floats and doubles parameter to control the representation of zero during initialization. See Use memset to initialize floats and doubles to 0.0.

  • The code does not initialize zero values whose storage classes have the Imported scope, regardless of the settings of the Remove internal data zero initialization and Remove root level I/O zero initialization parameters.

  • If you specify values (zero or nonzero) in an Initialize Function block, the code initializes those values, regardless of the settings of the Remove internal data zero initialization and Remove root level I/O zero initialization parameters.

  • If you connect an inport to an Initialize Function block, the code does not initialize a value for that inport, regardless of the setting of the Remove root level I/O zero initialization parameter.

Related Topics