Main Content

Control Generation of Initialization Code for Local Variables Set to Zero

By default, the code generator does not explicitly initialize local variables to zero. This produces more efficient code, but violates some coding standards such as MISRA C++:2008 Rule 0-1-4 (Polyspace Bug Finder). This example shows how to disable this optimization to remove initialization code from local variables set to zero.

Example Model

Open the LocalVariableZeroInitialization model. The model contains a subsystem that contains Sum and Gain blocks.

model = 'LocalVariableZeroInitialization';
open_system(model);

Generate Code with Optimization

Build the model by using Embedded Coder.

evalc('slbuild(model)');

This code is in the LocalVariableZeroInitialization.c file. The generated code does not initialize local variables to zero.

/* Model step function */
void LocalVariableZeroInitialization_step(void)
{
  real_T rtb_Gain4;
  real_T rtb_Sum1;

  /* Gain: '<S1>/Gain4' incorporates:
   *  Gain: '<S1>/Gain3'
   *  Inport: '<Root>/In3'
   */
  rtb_Gain4 = 2.0 * rtU.In3 * 2.0;

  /* Sum: '<S1>/Sum1' incorporates:
   *  Gain: '<S1>/Gain1'
   *  Gain: '<S1>/Gain5'
   *  Inport: '<Root>/In2'
   */
  rtb_Sum1 = 2.0 * rtU.In2 * 2.0 + rtb_Gain4;

  /* Outport: '<Root>/Out1' incorporates:
   *  Gain: '<S1>/Gain'
   *  Gain: '<S1>/Gain2'
   *  Inport: '<Root>/In1'
   *  Sum: '<S1>/Sum'
   */
  rtY.Out1 = (2.0 * rtU.In1 + rtb_Sum1) * 2.0;

  /* Outport: '<Root>/Out2' */
  rtY.Out2 = rtb_Sum1;

  /* Outport: '<Root>/Out3' */
  rtY.Out3 = rtb_Gain4;
}

Disable Optimization

Open the Configuration Parameters dialog box. Expand the Code Generation node and click Optimization. Point to the three dots and click Advanced parameters. Clear the Remove local variable initialization to zero value parameter.

Alternatively, you can use the MATLAB Command Window to disable the optimization. Set the model parameter RemoveLocalVariableInitialization to 'off'.

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

Generate Code Without Optimization

Build the model by using Embedded Coder.

evalc('slbuild(model)');

This code is in the LocalVariableZeroInitialization.c file. The generated code initializes local variables to zero.

/* Model step function */
void LocalVariableZeroInitialization_step(void)
{
  real_T rtb_Gain4 = 0.0;
  real_T rtb_Sum1 = 0.0;

  /* Gain: '<S1>/Gain4' incorporates:
   *  Gain: '<S1>/Gain3'
   *  Inport: '<Root>/In3'
   */
  rtb_Gain4 = 2.0 * rtU.In3 * 2.0;

  /* Sum: '<S1>/Sum1' incorporates:
   *  Gain: '<S1>/Gain1'
   *  Gain: '<S1>/Gain5'
   *  Inport: '<Root>/In2'
   */
  rtb_Sum1 = 2.0 * rtU.In2 * 2.0 + rtb_Gain4;

  /* Outport: '<Root>/Out1' incorporates:
   *  Gain: '<S1>/Gain'
   *  Gain: '<S1>/Gain2'
   *  Inport: '<Root>/In1'
   *  Sum: '<S1>/Sum'
   */
  rtY.Out1 = (2.0 * rtU.In1 + rtb_Sum1) * 2.0;

  /* Outport: '<Root>/Out2' */
  rtY.Out2 = rtb_Sum1;

  /* Outport: '<Root>/Out3' */
  rtY.Out3 = rtb_Gain4;
}

Close the model.

bdclose(model);

See Also

| (Polyspace Bug Finder)

Related Topics