Is it a strict aliasing violation to alias a struct as its first member?

The behaviour of the cast comes down to [expr.static.cast]/13; A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T”, where T is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1. If the original pointer value represents the address … Read more

reinterpret_cast between char* and std::uint8_t* – safe?

Ok, let’s get truly pedantic. After reading this, this and this, I’m pretty confident that I understand the intention behind both Standards. So, doing reinterpret_cast from std::uint8_t* to char* and then dereferencing the resulting pointer is safe and portable and is explicitly permitted by [basic.lval]. However, doing reinterpret_cast from char* to std::uint8_t* and then dereferencing … Read more

What happened to the “aggregate or union type that includes one of the aforementioned types” strict aliasing rule?

The lvalue you use to access the stored value of y is not *reinterpret_cast<Foo*>(&y), of type Foo, but it is reinterpret_cast<Foo*>(&y)->x, which has the type float. Accessing a float using an lvalue of type float is fine. In C++, you can not “access the value of a union or struct” (as whole), you can only … Read more

Does accessing the first field of a struct via a C cast violate strict aliasing?

First, it is legal to cast in C. §6.7.2.1/13: Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, … Read more

strict aliasing in C

They both violate the strict aliasing rule, I am going to quote my answer here which says (emphasis mine going forward): code violates the strict aliasing rules which makes it illegal to access an object through a pointer of a different type, although access through a char * is allowed. The compiler is allowed to … Read more

will casting around sockaddr_storage and sockaddr_in break strict aliasing

C and C++ compilers have become much more sophisticated in the past decade than they were when the sockaddr interfaces were designed, or even when C99 was written. As part of that, the understood purpose of “undefined behavior” has changed. Back in the day, undefined behavior was usually intended to cover disagreement among hardware implementations … Read more