|
|
|
|
|
by jancsika
1779 days ago
|
|
> The size of a union is sufficient to contain the largest of its members. Correct me if I'm wrong, but there is no part of the C spec that says this: When initializing a union member that is smaller than the largest member, the remaining bytes will always automatically be initialized to zero. If I'm right then the following caveat must be added to your statement: > A pointer to the union points to each of its members, and can be dereferenced to access it. ... if and only if the member which was originally initialized is at least as large as the other member being accessed. In other words, if you write your program in a way that ensures it will only compile when all union members are exactly the same size, and you have mandatory tooling to make sure that any changes to said union follow the same rule by force of compilation errors, then and only then can you claim what you claimed without the threat of undefined behavior. |
|