The following reasonable program seems to have undefined behavior according to the formal definition of restirct
in the C standard:
void positive_intcpy(int * restrict q, const int * restrict p, size_t n) {
int *qBgn = q;
const int *pEnd = p + n;
// sequence point S
while (p != pEnd && *p>0) *q++ = *p++;
if (q != qBgn) fprintf(stderr,"Debug: %d.
",*(q-1)); // undefined behavior!?
}
int main(void) {
int a[6] = {4,3,2,1,0,-1};
int b[3];
positive_intcpy(b,a,3);
return 0;
}
The function copies integers from one array to another, as long as the integers are positive. The fprintf
call displays the last positive integer that was copied (if any). There is never any aliasing between p
and q
.
这是否真的是“联邦”?