View previous topic :: View next topic |
Author |
Message |
libor
Joined: 14 Dec 2004 Posts: 288 Location: Hungary
|
evaluation order of parameters |
Posted: Sun Jul 30, 2006 11:51 am |
|
|
Can I rely on the evaluation order of a function's parameters ? will the parameters of any function (make16 in this case) evaluate always left to right ? Example:
Code: | ...
Var16 = make16(read_eeprom(Ptr++), read_eeprom(Ptr++));
...
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
libor
Joined: 14 Dec 2004 Posts: 288 Location: Hungary
|
|
Posted: Mon Jul 31, 2006 3:41 am |
|
|
I was just tempted to spare a temporary variable, not realizing, that the compiler allocates one anyway (?)
Although it worked as expected, I felt it is a bad practice, and you affirmed that. Thanks.
There's another (not evaluation-order-prone) dilemma: is it a good practice to squeeze as many intructions, operators as possible into one line (making a harder to read code), like
Code: | varB = array[ptr++] & 0x0F;
vs.
varB = array[ptr];
varB &= 0x0F;
ptr++; |
(I was just not trying to make a more complicated example, imagine one :-)
So my question is, that writing 'tight' (but less readable) code not specifying any particular order the compiler must evaluate the operators does help it optimizing the code ?
I suppose that preloading varB is unnecessary in the example, the compiler can do the '&=' instruction in the W register if it was not written in a separate line.
edited: when I looked at it, I saw my first example more readable :-), but anyway my question is still the same.
Last edited by libor on Mon Jul 31, 2006 9:42 am; edited 1 time in total |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Mon Jul 31, 2006 9:08 am |
|
|
Quote: | varB = array[ptr++] & 0x0F; |
I do this kind of stuff all the time. It's when your evaluation needs to start continuing onto the next line that it might be getting a bit long.
Ronald |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 31, 2006 10:33 am |
|
|
Quote: | varB = array[ptr++] & 0x0F; |
This is good coding style. There's nothing wrong with it. |
|
|
libor
Joined: 14 Dec 2004 Posts: 288 Location: Hungary
|
|
Posted: Mon Jul 31, 2006 12:27 pm |
|
|
Thank you all. Regarding the first problem, I've come to this:
Code: | long Var16;
int VarLo, VarHi;
#locate VarLo = &Var16
#locate VarHi = &Var16 + 1
VarHi = read_eeprom[Ptr++];
VarLo = read_eeprom[Ptr++]; |
Is this the cleanest solution ? Or should I use a union instead with the two 'int' variables overlapping the 'long' one ? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 31, 2006 2:06 pm |
|
|
In this case, a union of a 'long' and a struct (of two bytes) would be
considered better style. The reasons would be:
1. Better portability, in case you move to another compiler.
2. If someone else has to maintain your code in the future, it's easier
for them to understand if you use coding methods that are part of
standard C. |
|
|
|