A pointer is the address of a location in memory. More than one pointer can access the same chunk of memory and modify it during the course of a program. The restrict (or __restrict or __restrict__)1 type qualifier is an indication to the compiler that, if the memory addressed by the restrict -qualified pointer is modified, no other pointer will access that same memory. The compiler may choose to optimize code involving restrict -qualified pointers in a way that might otherwise result in incorrect behavior. It is the responsibility of the programmer to ensure that restrict -qualified pointers are used as they were intended to be used. Otherwise, undefined behavior may result.
void foo(int n, int * restrict a, int * restrict b, int * restrict c)
{
int i;
for (i = 0; i < n; i++)
a[i] = b[i] + c[i];
}
{
int * restrict x;
int * restrict y;
x = y; // undefined
{
int * restrict x1 = x; // okay
int * restrict y1 = y; // okay
x = y1; // undefined
}
}
In nested blocks containing restricted pointers, only
assignments of restricted pointers from outer to inner blocks are
allowed. The exception is when the block in which the restricted pointer
is declared finishes execution. At that point in the program, the
value of the restricted pointer can be carried out of the block in
which it was declared.
The restrict keyword is recognized under
compilation with xlc or c99 or with
the -qlanglvl=stdc99 or -qlanglvl=extc99 options
or -qkeyword=restrict.
The __restrict and __restrict__ keywords
are recognized at all language levels.
The restrict, __restrict and __restrict__ keywords
are recognized at -qlanglvl=extended.
The restrict keyword is also recognized at other
language levels with -qkeyword=restrict.