...
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 defined behavior */
} |
Noncompliant Code Example
...
Code Block | ||||
---|---|---|---|---|
| ||||
void h(size_t n, int * restrict p, int * restrict q, int * restrict r) {
for (size_t i = 0; i < n; i++)
p[i] = q[i] + r[i];
}
void j(void) {
int a[100];
h(100, a, a, a); /* undefined behavior */
} |
The function g()
declares an array d
consisting of 100 int
values and then invokes f()
to copy memory from one area of the array to another. This call has undefined behavior because each of d[1]
through d[49]
is accessed through both p
and q
.
...
Code Block | ||||
---|---|---|---|---|
| ||||
void h(size_t n, int * restrict p, int * restrict q, int * restrict r) { for (size_t i = 0; i < n; i++) p[i] = q[i] + r[i]; } void j(void) { int a[100]; int b[100]; h(100, a, b, b); /* valid defined behavior */ } |
Invoking Library Functions with restrict-qualified Pointers
...