Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does copying data byte wise between types break strict aliasing?

Tags:

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?