Suppose I have 2 types A
and B
with same size and I have two variables
A a = ... ; // Initialized to some constant of type A
B b;
If I copy the contents of a
to b
using something like -
assert(sizeof(A) == sizeof(B));
size_t t;
for( t=0; t < sizeof(A); t++){
((char*)&b)[t] = ((char*)&a)[t];
}
Does this break strict aliasing rules of C?
I know casting a pointer to char*
and reading it is not UB but I am concerned about both the derefences involved in the assignment.
If this is not UB, can this be a valid way for type punning?
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