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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
09/08/06 00:32
Read: times


 
#123891 - Think about your variable types
Responding to: ???'s previous message
int  pos;          //position within array
char Length = 0xA; //Length of frame, excluding SOF and Checksum

pos is an index to a 10-element array; so it only needs to be a 'char'; not an 'int'
Most (all?) 8051 compilers will use 16 bits for an 'int' - which is a significant unnecessary overhead when you don't need it.

Similarly, you are only using positive values; so it can be 'unsigned'
The 8051 has no native instructions for signed arithmetic so, again, it's an unnecessary overhead when you don't need it:
unsigned char pos;          //position within array
unsigned char Length = 0xA; //Length of frame, excluding SOF and Checksum


Note that I expressed some doubt above about whether your compiler might use 16 bits for an 'int'. This is because the 'C' language does not define specific sizes for the different types.
Therefore it's best to avoid the standard language types, and define your own of known size & signed-ness; eg,
typedef unsigned char U8 ; // Unsigned,  8 bits
typedef signed   char S8 ; // Signed,    8 bits

typedef unsigned int  U16; // Unsigned, 16 bits
typedef signed   int  S16; // Signed,   16 bits

When you start using a new compiler, you have to look-up in the Manual to find the specific type to use in each definition.
You would put these in a header file, and just #include it everywhere...



List of 24 messages in thread
TopicAuthorDate
^= , Checksum, Problem            01/01/70 00:00      
   Have you tried a simulator?            01/01/70 00:00      
      well,            01/01/70 00:00      
      OK            01/01/70 00:00      
   volatile?            01/01/70 00:00      
      volatile            01/01/70 00:00      
         using ICE ?            01/01/70 00:00      
            update code (working) and clarification            01/01/70 00:00      
               Style            01/01/70 00:00      
               Think about your variable types            01/01/70 00:00      
                  Thanks            01/01/70 00:00      
                     Magic numbers            01/01/70 00:00      
                        but don't be "oversmart"            01/01/70 00:00      
                           example?            01/01/70 00:00      
                           advantages            01/01/70 00:00      
                              the most often forgotten quality guarantee            01/01/70 00:00      
                     so, use structures!            01/01/70 00:00      
                        padding            01/01/70 00:00      
                   and             01/01/70 00:00      
                     Actual Output            01/01/70 00:00      
                     C99            01/01/70 00:00      
                        making up your own            01/01/70 00:00      
                           Names            01/01/70 00:00      
                     FYI - C99 Exact- & Minimum-width types            01/01/70 00:00      

Back to Subject List