After reading the C standard, 6.7.3.1 "Formal definition of restrict
", i have the following misunderstanding. I wonder if the following code instantly causes undefined behavior:
void foo(int *restrict p) {
int *restrict q = p;
}
It is clear that q
is assigned the value that is based on another restricted pointer p
. What is unclear is if these two pointers are associated with the same block (the function itself), or with different blocks (p
with the function itself, q
with its compound statement body), because, eg.
int *restrict p;
{
int *restrict q = p;
}
does not cause undefined behavior (it is OK to create aliasing pointers in sub-blocks).
The top answer in MSVC++ restrict keyword and local variables suggests that int *restrict q = p + 1;
would be fine, however [at least, in case of the C standard] it's not true, because expression p + 1
is still based on p
.
The definition of the block is in 6.7.3.1 p2:
- If [a certain pointer declaration]
D
appears inside a block and does not have storage classextern
, letB
denote the block. IfD
appears in the list of parameter declarations of a function definition, letB
denote the associated block. [...]
So is "the associated block" of the function the same thing as the function body in this context? Because i didn't instantly find any explanation of the term "associated block" earlier in the text.