Restricted pointers in C99
The C99 keyword restrict is an indication to
the compiler that different object pointer types and function parameter
arrays do not point to overlapping regions of memory. This enables
the compiler to perform optimizations that might otherwise be prevented
because of possible aliasing.
In the following example, pointer a does
not, and must not, point to the same region of memory as pointer b:
void copy_array(int n, int *restrict a, int *restrict b)
{
while (n-- > 0)
*a++ = *b++;
}
void test(void)
{
extern int array[100];
copy_array(50, array + 50, array); // valid
copy_array(50, array + 1, array); // undefined behavior
}
Pointers qualified with restrict can however
point to different arrays, or to different regions within an array.
It is your responsibility to ensure that restrict-qualified
pointers do not point to overlapping regions of memory.
__restrict, permitted in C90 and C++, is a synonym
for restrict.
--restrict enables restrict to
be used in C90 and C++.
See also