Please consider the following code:
typedef struct {
int type;
} object_t;
typedef struct {
object_t object;
int age;
} person_t;
int age(object_t *o
The strict aliasing rule limits by what types you access an object (a region of memory). There are a few places in the code where the rule might crop up: within age() and when calling age().
Within age, you have object to consider. ((person_t *)object) is an lvalue expression because it has an object type and it designates an object (a region of memory). However, the branch is only reached if object->type == PERSON, so (presumably) the effective type of the object is a person_t*, hence the cast doesn't violate strict aliasing. In particular, strict aliasing allows:
- a type compatible with the effective type of the object,
When calling age(), you will presumably be passing an object_t* or a type that descends from object_t: a struct that has an object_t as the first member. This is allowed as:
- an aggregate or union type that includes one of the aforementioned types among its members
Furthermore, the point of strict aliasing is to allow for optimizing away loading values into registers. If an object is mutated via one pointer, anything pointed to by pointers of an incompatible type are assumed to remain unchanged, and thus don't need to be reloaded. The code doesn't modify anything, so shouldn't be affected by the optimization.