I know about the memcpy/memmove to a union member, does this set the 'active' member? question , but I guess my question is different. So:
Suppose sizeof( int ) == sizeof( float )
and I have the following code snippet:
union U{
int i;
float f;
};
U u;
u.i = 1; //i is the active member of u
::std::memcpy( &u.f, &u.i, sizeof( u ) ); //copy memory content of u.i to u.f
My questions:
- Does the code lead to an undefined behaviour (UB)? If yes why?
- If the code does not lead to an UB, what is the active member of
u
after thememcpy
call and why? - What would be the answer to previous two questions if
sizeof( int ) != sizeof( float )
and why?