An object that is accessed through a restrict-qualified pointer has a special association with that pointer. This association requires that all accesses to that object use, directly or indirectly, the value of that particular pointer. The intended use of the restrict qualifier is to promote optimization, and deleting all instances of the qualifier from a program does not change its meaning (that is, observable behavior). In the absence of this qualifier, other pointers can alias this object. Cacheing the value in an object designated through a restrict-qualified pointer is safe at the beginning of the block in which the pointer is declared, because no pre-existing aliases may also be used to reference that object. The cached value must be restored to the object by the end of the block, where pre-existing aliases again become available. New aliases may be formed within the block, but these must all depend on the value of the restrict-qualified pointer, so that they can be identified and adjusted to refer to the cached value. For a restrict-qualified pointer at file scope, the block is the body of each function in the file [Douglas Walls 2006].
Overlapping Objects
The file scope declarations assert that if an object is accessed using one of a , b , or c , and that object is modified anywhere in the program, then it is never accessed using either of the other two.
Code Block |
---|
int * restrict a;
int * restrict b;
extern int c[];
int main(void) {
a = c[0] = 17;
b = c[1] = 18;
*a = *b; /* undefined behavior */
} |
restrict-qualified pointers Function Parameters
Code Block |
---|
void f(int n, int * restrict p, int * restrict q) {
while (n-- > 0)
*p++ = *q++;
}
void g(void) {
extern int d[100];
f(50, d + 50, d); //
valid
f(50, d + 1, d); //
undefined behavior
} |
Code Block | ||||
---|---|---|---|---|
| ||||
void f(int n, int * restrict p, int * restrict q) {
while (n-- > 0)
*p++ = *q++;
}
void g(void) {
extern int d[100];
f(50, d + 50, d); // valid
} |
Invoking Library Functions with restrict-qualified Pointers
...
Douglas Walls. How to Use the Qualifier in C. Sun ONE Tools Group, Sun Microsystems, July 2003 (revised March 2006)
...