8.13 restrict

The restrict keyword is a C99 feature. It enables you to convey a declaration of intent to the compiler that different pointers and function parameter arrays do not point to overlapping regions of memory at runtime.

This enables the compiler to perform optimizations that can otherwise be prevented because of possible aliasing.

Usage

The keywords __restrict and __restrict__ are supported as synonyms for restrict and are always available.
You can specify --restrict to allow the use of the restrict keyword in C90 or C++.

Restrictions

The declaration of intent is effectively a promise to the compiler that, if broken, results in undefined behavior.

Examples

The following example shows use of the restrict keyword applied to function parameter arrays.
void copy_array(int n, int *restrict a, int *restrict b)
{
    while (n-- > 0)
        *a++ = *b++;
}
The following example shows use of the restrict keyword applied to different pointers that exist in the form of local variables.
void copy_bytes(int n, int *a, int *b)
{
    int *restrict x;
    int *restrict y;
    x = a;
    y = b;
    while (n-- > 0)
        *q++ = *s++;
}
Related concepts
4.59 New language features of C99
Related reference
7.145 --restrict, --no_restrict
Non-ConfidentialPDF file icon PDF versionARM DUI0375F
Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved.