Main Content

Qualify C restrict function arguments

Annotate function declarations with the restrict keyword

Since R2026a

Description

App Configuration Pane: Speed

Configuration Objects: | coder.EmbeddedCodeConfig

Whether to apply the C restrict qualifier to function arguments when it might enable additional compiler optimizations. This parameter requires Embedded Coder®.

Settings

Off

This is the default setting.

Does not apply the restrict keyword automatically.

On

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

Programmatic Use

Property: QualifyRestrictAuto
Values: false | true
Default: false

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.

Prepare Example Function for Code Generation

For this example, you generate C code for the example function addMatrix.m.

type addMatrix.m
function [S] = addMatrix(A,B) %#codegen
S = zeros(size(A));
for row = 1:size(A,1) 
    for col = 1:size(A,2)  
        S(row,col) = A(row,col)+B(row,col);
    end
end

Configure the code generator to use the C99 language standard. Open the MATLAB Coder app and, in the Code Generation Settings dialog box, set these parameters:

  • Language standard to C99 (ISO)

  • Generate source code only to on

Alternatively, set the parameters at the command line by using a coder.EmbeddedCodeConfig object.

cfg = coder.EmbeddedCodeConfig;
cfg.GenCodeOnly = 1;
cfg.TargetLangStandard = 'C99 (ISO)';

Generate Code Without Restrict Keyword

Generate code from the function.

codegen addMatrix -args {ones(20,10),ones(20,10)} -config cfg -report -rowmajor
Code generation successful: View report

The generated function does not use the restrict keyword.

coder.example.extractLines("codegen/lib/addMatrix/addMatrix.c", ...
    "void addMatrix", "int col")
void addMatrix(const double A[200], const double B[200], double S[200])
{

Generate Code With Restrict Keyword

Configure the code generator to generate code that uses the restrict keyword. In the Code Generation Settings dialog box, on the Speed pane, turn on Qualify C restrict function arguments. Alternatively, at the command line, set the configuration parameter QualifyRestrictAuto to on.

cfg.QualifyRestrictAuto = "on";
codegen addMatrix -args {ones(20,10),ones(20,10)} -config cfg -report -rowmajor
Code generation successful: View report

The generated function uses the restrict keyword.

coder.example.extractLines("codegen/lib/addMatrix/addMatrix.c", ...
    "void addMatrix", "int col")
void addMatrix(const double *__restrict A, const double *__restrict B,
               double *__restrict S)
{

Version History

Introduced in R2026a