0

In the following code, can the value of int be predicted ( how ? ), or it is just the garbage ?

union a
{
    int i;
    char ch[2];
};
a u;
u.ch[0] = 0;
u.ch[1] = 0;
cout<<u.i;
}
11
  • 1
    Its undefined behavior, but it might still work.
    – askmish
    Aug 10, 2012 at 7:35
  • 1
    @R.MartinhoFernandes: Somewhat related, but not a duplicate at all, in my opinion.
    – Gorpik
    Aug 10, 2012 at 7:39
  • @Gorpik what's the difference? Aug 10, 2012 at 7:40
  • @R.MartinhoFernandes: For once, this deals with a union, the other question with a plain int. Additionally, the chars in the union don't necessarilty cover the same memory as the int (in most implementations they actually don't). As Luchian and askmish correctly state, this is UB, while the other is not.
    – Gorpik
    Aug 10, 2012 at 7:43

1 Answer 1

3

I would say that depends on the size of int and char. A union contains the memory of the largest variable. If int is 4 bytes and char[2] represents 2 bytes, the int consumes more memory than the char-array, so you are not initialising the full int-memory to 0 by setting all char-variables. It depends on your memory initialization mechanisms but basically the value of the int will appear to be random as the extra 2 bytes are filled with unspecified values.

Besides, filling one variable of a union and reading another is exactly what makes unions unsafe in my oppinion.

If you are sure that int is the largest datatype, you can initialize the whole union by writing

union a
{
    int i;
    char ch[2];
};

void foo()
{
    a u = { 0 };  // Initializes the first field in the union
    cout << u.i;
}

Therefore it may be a good idea to place the largest type at the beginning of the union. Althugh that doesn't garantuee that all datatypes can be considered zero or empty when all bits are set to 0.

5
  • And it's coincidentally one of the two primary uses of union. (The other being simple polymorphism, which is irrelevant in C++.) Aug 10, 2012 at 7:52
  • In C++, that use of a union is quite useless, since it can be replaced by reinterpret_cast. Really useful in C though.
    – Morwenn
    Aug 10, 2012 at 7:55
  • The result also depends on the processor's endianness. Different results will be observed, when the code is compiled on MIPS (where char[0] will be the most significant byte) and x86 processors (that are little endian and char[0] will be the least significant byte) for example. Aug 10, 2012 at 7:57
  • 1
    Accessing a member of the union other than the last one stored into does not cause undefined behavior. Per C 1999 6.2.6.1 7, the bytes of a member that do not correspond to bytes of the member stored into take unspecified values. Thus, the program must behave as if the member has some value; which is different from undefined behavior because the latter permits any behavior. (There can be additional complications from trap representations, which I will not detail here, and they generally do not apply to simple integer types.) Aug 10, 2012 at 10:55
  • You are right, thanks, it's not really undefined behavior. I edited my answer. I just thought of it as undefined behavior because the program might expect the int to be initialized to 0, while the value can be anything, really.
    – Excelcius
    Aug 10, 2012 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.