|
|
#1 |
|
Swordsman
Join Date: Apr 2008
Posts: 450
![]() |
[3.4-RC] Macro problem
Just a little reminder for C devs: be careful when using macros evaluating variables more than once, because unwanted effects could occur. I've screwed myself more than once with such macros...
Here's a good example from mon-spell.c: Code:
inven_damage(p_ptr, re_ptr->gf, MIN(dam * randcalc(re_ptr->dam, 0, RANDOMISE), 300)); Code:
#define MIN(a,b) (((a) > (b))? (b) : (a)) In the previous example, nothing can ensure you that the result will be lower than 300, since randcalc will be called twice, once to compare the result to 300 and once to return the result! The fix is simple: Code:
int calcdam = dam * randcalc(re_ptr->dam, 0, RANDOMISE); inven_damage(p_ptr, re_ptr->gf, MIN(calcdam, 300));
__________________
PWMAngband variant maintainer - check http://www.mangband.org/forum/viewforum.php?f=9 to learn more about this new variant! |
|
|
|
|
|
#2 |
|
Prophet
Join Date: Dec 2009
Posts: 4,741
![]() |
As noted in another thread, it'd probably be a good idea to write macro names in ALL_CAPS or some other distinguishing method. It won't guarantee that you won't make this mistake, but it should help remind you that you aren't making a normal function call.
And of course, where possible macros should be replaced by function calls. I don't think even Angdroid needs the minor performance improvements macros get you. |
|
|
|
|
|
#3 |
|
Adept
Join Date: Jan 2008
Posts: 164
![]() |
Won't good compilers inline the kinds of functions you'd make macros in the first place?
|
|
|
|
|
|
#4 |
|
Angband Devteam member
|
Looks like this ought to be my job, since PowerWyrm's example (and all the many ones like it) are my errors. It might explain quite a few things!
Opened as #1668.
__________________
"3.2 is way too easy. 3.3 is not much better, but it is a step to right direction." - Timo Pietila |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Macro help | Raggy | Vanilla | 11 | June 19, 2011 07:05 |
| Macro Problem | Larvitz | Vanilla | 6 | January 21, 2009 20:59 |
| Macro Problem? | cerberus | Vanilla | 3 | October 8, 2008 09:45 |
| Macro problem | Zero | Vanilla | 2 | June 20, 2008 14:52 |
| Macro problem | shevek | Vanilla | 9 | May 19, 2008 20:43 |