Previous Table of Contents "New C Standard" commentary
1502
Let
1503
If
1504
If
1505
Otherwise, let
1506
In what follows, a pointer expression
1507 Note that based is defined only for expressions with pointer types.
1508
During each execution of
1509
If
1510
Every other lvalue used to access the value of
1511
Every access that modifies
1512
If
1513 If these requirements are not met, then the behavior is undefined.
1514
Here an execution of
1515
A translator is free to ignore any or all aliasing implications of
uses of
1516 EXAMPLE 1 The file scope declarations
int * restrict a;
int * restrict b;
extern int c[];
assert that if an object is accessed using one of
1517
117) In other words,
1518
For example, if identifier
1519 EXAMPLE 2 The function parameter declarations in the following example
void f(int n, int * restrict p, int * restrict q)
{
while (n-- > 0)
*p++ = *q++;
}
assert that, during each execution of the function, if an object is accessed through one of the pointer parameters, then it is not also accessed through the other.
The benefit of the
void g(void)
{
extern int d[100];
f(50, d + 50, d); // valid
f(50, d + 1, d); // undefined behavior
}
1520 EXAMPLE 3 The function parameter declarations
void h(int n, int * restrict p, int * restrict q, int * restrict r)
{
int i;
for (i = 0; i < n; i++)
p[i] = q[i] + r[i];
}
illustrate how an unmodified object can be aliased through two
restricted pointers. In particular, if
1521 EXAMPLE 4 The rule limiting assignments between restricted pointers does not distinguish between a function call and an equivalent nested block. With one exception, only outer-to-inner assignments between restricted pointers declared in nested blocks have defined behavior.
{
int * restrict p1;
int * restrict q1;
p1 = q1; // undefined behavior
{
int * restrict p2 = p1; // valid
int * restrict q2 = q1; // valid
p1 = q2; // undefined behavior
p2 = q2; // undefined behavior
}
}
The one exception allows the value of a restricted pointer to be
carried out of the block in which it (or, more precisely, the
ordinary identifier used to designate it) is declared when that block
finishes execution. For example, this permits
typedef struct { int n; float * restrict v; } vector;
vector new_vector(int n)
{
vector t;
t.n = n;
t.v = malloc(n * sizeof (float));
return t;
}
Next
Created at: 2008-01-30 02:39:43
The text from WG14/N1256 is copyright © ISO