Poll: does this violate strict aliasing? struct b { int x; } S; int *p = (int*)&S; *p = 1;
-
-
I'm not sure what this paragraph means by “points to its initial member”. One could argue that when x is an int *, (void**)&x “points to” x…
-
that does not mean that you are allowed to dereference it to access a, only that if you convert both to void* and compare them, they're ==
-
If p "points to" x, and *p has type matching the effective type of x, then p meets conditions for * operator to be well-defined on it.
-
Ok I can't produce a counter-example where a compiler will optimize more aggressively than this interpretation allows, so we'll use this.
-
For reference, I think GCC devs claim it's &p->a that's wrong if one passes the address of an int or a struct s2 https://godbolt.org/g/0iLBjc
-
6.7.2.1 ¶15 ends with "and vice versa", i.e. if you have a ptr to initial member, it's valid to cast to struct ptr & access the struct.
-
(But only if the pointer actually points to the initial member of a struct of that type, not to a different struct type or no struct at all)
-
Yes, that seems the right way to look at it.
- 1 more reply
New conversation -
-
-
oh, ha. So this is what allows struct sockaddr casting not to be UB...
-
That's different and it is UB if you use sockaddr.
-
how? if you check af_family and only used subsequent fields for same type, then this seems to fall under initial member rules
-
(*(struct sockaddr *)sin).sa_family is not the same as *(sa_family_t *)sa. Latter is well-defined C but assumes sa_family is first member.
-
The former is an aliasing violation because it accesses the object pointed to by sin with the wrong type, struct sockaddr.
-
ah, but accessing the family directly as (sa_family_t*)sockaddr would be okay I guess.
-
Yes, but I don't think POSIX imposes a requirement that sa_family be the first member...
-
You could do *(sa_family_t*)((char *)p+offsetof(struct sockaddr, sa_family)) :-)
- 3 more replies
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.