Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
03/23/10 07:24
Read: times


 
Msg Score: +1
 +1 Good Answer/Helpful
#174416 - Code for transparency
Responding to: ???'s previous message
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.

Greate care - and thought - should be given when instead trying to store "both" variable type A and variable B at the same time, i.e. when trying to use unions for type conversion. The "clever" or "beautiful" code is more fragile, and isn't as simple to read.

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].

Working with an 8-bit processor will allow some abuse that may change drastically with other processors. A 32-bit processor may add 2 pad bytes to make the data type n*4 bytes large. And if one or more of inidivudal fields are larger than a single byte, then there may be padding between the individual fields in a struct. And for fields larger than a byte, the byte order within the field will matter.

In the end, you normally get the lowest maintainance costs by striving for great transparency. Writing code that does exactly what it looks like it is doing. Yes - sometimes you have to write a couple of extra characters. But the efficiency of the program isn't proportional to the number of characters in the source code.

List of 23 messages in thread
TopicAuthorDate
Unions and position of bytes            01/01/70 00:00      
   Syntax and strategy problem.            01/01/70 00:00      
      wow that was quick and excellent thanks            01/01/70 00:00      
      Works a treat            01/01/70 00:00      
   Note that this is heavily compiler-reliant            01/01/70 00:00      
      Code for transparency            01/01/70 00:00      
         Agreed - after bitter experience            01/01/70 00:00      
            Compiler Specific            01/01/70 00:00      
               How many compler brands/versions to test for?            01/01/70 00:00      
                  Create a Compiler header            01/01/70 00:00      
                     Didn't we talk about unions - your examples doesn't...            01/01/70 00:00      
                        Someone already wrote up a good way....            01/01/70 00:00      
                           Incomplete byte order. But pad is still dangerous            01/01/70 00:00      
                              why bother?            01/01/70 00:00      
                                 how would you know?            01/01/70 00:00      
                                    Exactly my point... you don't            01/01/70 00:00      
                                       Never give up on portability - just decide the amount            01/01/70 00:00      
               Yes, but            01/01/70 00:00      
               why bother?            01/01/70 00:00      
      unions and portability            01/01/70 00:00      
         OT: use TR0 = 0; TMR0 -= offset; TR0 = 1;            01/01/70 00:00      
         you're not?            01/01/70 00:00      
            It was just a warning that union type casts are dangerous            01/01/70 00:00      

Back to Subject List