??? 03/23/10 08:19 Read: times |
#174417 - Agreed - after bitter experience Responding to: ???'s previous message |
Per Westermark said:
I think that unions are used too often for clever attempts at "cleaning up" code.
They are originally intended for storing "either" varable A or variable B or variable C in the same memory space. Strictly, the effect of writing a union as variable A and then reading it as variable B is undefined. In practice, all compilers I've used will overlay the different "variants" of the union - so using it for type conversion will "work" (sic). In this case, it would be easier to create an enumeration naming the indices of the different array entries, and instead access my_time[MINUTES]. Agreed. A 32-bit processor may add 2 pad bytes to make the data type n*4 bytes large. You can try to get around this by using compiler-specific extensions such as a "packed" option on the union - but you can then get into real trouble with unaligned accesses... You generally escape all these issues on an 8-bit processor, which can lead you to become "lazy" - because it all just "works" (sic). But, if you then try to adopt the same approach on a 32-bit processor, it is quite likely to fall apart in your hands. Well, that's what I found, anyhow! |