The accepted answer to What is the strict aliasing rule? mentions that you can use char *
to alias another type but not the other way.
It doesn't make sense to me — if we have two pointers, one of type char *
and another of type struct something *
pointing to the same location, how is it possible that the first aliases the second but the second doesn't alias the first?
"Strict aliasing is an assumption, made by the C (or C++) compiler, that dereferencing pointers to objects of different types will never refer to the same memory location (i.e. alias each other.)"
In both C and C++ the standard specifies which expression types are allowed to alias which types. The compiler and optimizer are allowed to assume we follow the aliasing rules strictly, hence the term strict aliasing rule.
if we have two pointers, one of type
char *
and another of typestruct something *
pointing to the same location, how is it possible that the first aliases the second but the second doesn't alias the first?
It does, but that's not the point.
The point is that if you have one or more struct something
s then you may use a char*
to read their constituent bytes, but if you have one or more char
s then you may not use a struct something*
to read them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With