On Saturday 2010-06-12 20:22, Ian Lance Taylor wrote: >>>> I hear that >>>> >>>> "C specifies that a struct * can be converted to and from a >>>> pointer to its first element." >>>> >>>> Does that change anything? >>> >>>Yes, it should generally be OK to cast between a pointer to a struct >>>type and a pointer to the type of the first element of the struct. >> >> So could it be that the aliasing optimizations are overzealous here? > >I don't know, because that wasn't what you were doing. Quite the same linked-list traversal as before. We established that it has an aliasing violation due to accessing members through the pointer obtained by ((struct item *)head.next). head.next equals &itm.list, which is a pointer to the type of the first element in struct item. So the conversion to struct item * should be ok, as we seem to agree on. If the conversion is allowed (in this specific piece of code), then this should not cause an aliasing violation with subsequent segfault, should it? struct list_head { struct list_head *next; }; struct item { struct list_head list; }; int main(void) { struct list_head hdr; struct item itm, *pos; hdr.next = &itm.list; itm.list.next = &hdr; for (pos = (struct item *)hdr.next; pos != (struct item *)&hdr; pos = (struct item *)(pos->list.next)) ; return 0; }