Main Content

Use C restrict qualifier

Insert C restrict keyword for function arguments

Since R2026a

Model Configuration Pane: Code Generation / Optimization

Description

The Use C restrict qualifier parameter instructs the code generator to apply the restrict keyword to function arguments when it might enable additional compiler optimizations.

Dependencies

To enable this parameter:

Settings

off (default) | on

On

Applies the restrict keyword to pointer arguments automatically. The code generator does not apply the restrict keyword if the pointer is aliased.

Off

Does not apply the restrict keyword automatically.

Examples

expand all

This example shows how to generate code that uses the C restrict keyword. Applying the restrict keyword to pointer arguments can enable additional compiler optimizations that can improve the execution efficiency of the code.

Open And Prepare Example Model

Open the example model ReusableStorageClass.slx. The model contains multiple subsystems for which the code generator generates functions that use arguments.

model = "ReusableStorageClass";
open_system(model)

Configure the model to use the C99 language standard. Open the model configuration parameters and set Language standard to C99 (ISO), then click OK. Alternatively, set the parameter at the command line.

set_param(model,"TargetLangStandard","C99 (ISO)")

Generate Code Without Restrict Keyword

Generate code from the model.

set_param(model,"UseRestrictQualifier","off");
slbuild(model);
### Searching for referenced models in model 'ReusableStorageClass'.
### Total of 1 models to build.
### Starting build procedure for: ReusableStorageClass
### Successful completion of build procedure for: ReusableStorageClass

Build Summary

Top model targets:

Model                 Build Reason                                         Status                        Build Duration
=======================================================================================================================
ReusableStorageClass  Information cache folder or artifacts were missing.  Code generated and compiled.  0h 0m 21.767s

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 23.808s

The generated functions for the subsystems do not use the restrict keyword.

coder.example.extractLines("ReusableStorageClass_ert_rtw/ReusableStorageClass.c", ...
    "static void DeltaX", "static void DeltaSubsystem")
static void DeltaX(const real_T rtu_u1[262144], const real_T rtu_u2[262144],
                   real_T rty_z1[261632], real_T rty_z2[261632]);
static void DeltaY(const real_T rtu_u1[261632], const real_T rtu_u2[261632],
                   real_T rty_z1[261121], real_T rty_z2[261121]);
static void Downsample(const real_T rtu_u1[1048576], const real_T rtu_u2[1048576],
  real_T rty_z1[262144], real_T rty_z2[262144]);

Generate Code With Restrict Keyword

Configure the model to generate code that uses the restrict keyword. In the model configuration parameters, on the Code Generation > Optimization pane, turn on Specify custom optimizations. In the Details section, turn on Use C restrict Qualifier and click OK. Alternatively, set the parameters from the command line.

set_param(model,"OptimizationCustomize","on");
set_param(model,"UseRestrictQualifier","on");

Generate code from the model.

slbuild(model);
### Searching for referenced models in model 'ReusableStorageClass'.
### Total of 1 models to build.
### Starting build procedure for: ReusableStorageClass
### Successful completion of build procedure for: ReusableStorageClass

Build Summary

Top model targets:

Model                 Build Reason                     Status                        Build Duration
===================================================================================================
ReusableStorageClass  Generated code was out of date.  Code generated and compiled.  0h 0m 9.0945s

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 9.7476s

The generated functions for the subsystems use the restrict keyword.

coder.example.extractLines("ReusableStorageClass_ert_rtw/ReusableStorageClass.c", ...
    "static void DeltaX", "static void DeltaSubsystem")
static void DeltaX(const real_T *__restrict rtu_u1, const real_T *__restrict
                   rtu_u2, real_T *__restrict rty_z1, real_T *__restrict rty_z2);
static void DeltaY(const real_T *__restrict rtu_u1, const real_T *__restrict
                   rtu_u2, real_T *__restrict rty_z1, real_T *__restrict rty_z2);
static void Downsample(const real_T *__restrict rtu_u1, const real_T *__restrict
  rtu_u2, real_T *__restrict rty_z1, real_T *__restrict rty_z2);

Recommended Settings

ApplicationSetting
DebuggingNo impact
TraceabilityNo impact
EfficiencyOn
Safety precautionOff

Programmatic Use

Parameter: UseRestrictQualifier
Type: character vector
Value: 'on' | 'off'
Default: 'off'

Version History

Introduced in R2026a