Firstly, cppreference has the following conditions:
For each execution of a block in which a bounded pointer P is declared (usually each execution of the body of the function in which P is a parameter of the function), if any object accessible through P (directly or indirectly) changes, by any means, all calls to this the object (both reading and writing) in this block must occur through P (directly or indirectly), otherwise the behavior is undefined.
But below this paragraph it says:
Limited pointers can be freely assigned to unlimited pointers; optimization options remain in place while the compiler is able to parse the code:
void f(int n, float * restrict r, float * restrict s) { float * p = r, * q = s;
In the above example, r [0] is an object accessible via the restricted pointer r, and it is accessed via p, which seems to contradict the first paragraph (access to r [0] should occur exclusively through r)?
Secondly:
Assigning from one restricted pointer to another is undefined behavior, unless you assign a pointer to an object in some external block to a pointer in some internal block (including using the argument with a restricted pointer when calling a function with a limited pointer parameter) or when returning from a function (and otherwise, when it has finished the pointer block).
So, is the following code ok?
void foo(char* restrict a, size_t s) { for(char* restrict aa = a; aa < a + s; ++aa)
source share