int a[3], * restrict ip = &a[1]; *ip = 1; does a; invoke undefined behavior?
Printable View
int a[3], * restrict ip = &a[1]; *ip = 1; does a; invoke undefined behavior?
No. There is no undefined behavior. Maybe there might be a compile error.
why do you think that?
Not sure what the question is? Clarify the question and and the code snippet.
Does it violate the guarantees of restrict? Is a; considered an access that conflicts with the modification of a[1] through a restrict qualified pointer?
how do you know?
The way I see it, a restrict pointer means that only that pointer will be used to access the object(s) which it points to. In this case the object is the integer at a[1]. So if a[1] is accessed while ip is in scope, then behavior is undefined. Same thing goes for a[2] if *(ip+1) is accessed. You can access a[0] during the lifetime of ip, as long as you don't access *(ip-1).
gg
yes, I agree. but can you do 'a;'? note the difference between that and 'a[0];'. does 'a;' count as an access to a[0], a[1], and a[2]?
a is just an address (and the compiler can already track it's accesses). It would need to be dereferenced to be an "integer object" access.
gg
but before it's an address, it's an array object, correct? only then would it undergo array to pointer conversion.
let me put it another way...
struct S { int i; char c; };
S s = { 0 }, * restrict sp = &s; sp->i = 1;
would s; invoke undefined behavior? sure, it wouldn't need the values of any of its members in that statement, but what about this statement?
S s2 = s;
surely that would be an access of s, and violating sp's restrict qualifier, right? if "S s2 = s;" invokes undefined behavior, why wouldn't "s;"? why wouldn't 'a;' invoke undefined behavior if "s;" invokes undefined behavior?
>> would s; invoke undefined behavior?
I would say no. Because the compiler is not obligated to generate any real "access" (load or store) of "s".
The ip/sp examples aren't exactly the same:
The object that "sp" points to is "s".
The object that "ip" points to is "a[1]".
Access to the object "a[1]" is restricted only through "ip". So a, *a, *(a+2) are all ok and defined while ip is in scope.
Access to the object "s" is restricted only through "sp", but the expression "s;" doesn't constitute an access.
>> S s2 = s;
>> surely that would be an access of s
Yep.
gg
ah yes, I meant
struct S { int i; char c; };
S s = { 0 };
char * restrict cp = &s.c; *cp = 1;
your reasoning seems sane. thanks.