Go Back   Angband Forums > Angband > Vanilla

Thread Tools Display Modes
Old August 1, 2012, 15:40   #1
PowerWyrm's Avatar
Join Date: Apr 2008
Posts: 2,806
PowerWyrm is on a distinguished road
[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:

inven_damage(p_ptr, re_ptr->gf, MIN(dam * randcalc(re_ptr->dam, 0, RANDOMISE), 300));
The MIN macro is defined as:

#define MIN(a,b)    (((a) > (b))? (b)   : (a))
So writing something like MIN(random, fixed) is actually (random > fixed? fixed: random).

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:

int calcdam = dam * randcalc(re_ptr->dam, 0, RANDOMISE);
inven_damage(p_ptr, re_ptr->gf, MIN(calcdam, 300));
PWMAngband variant maintainer - check (or to learn more about this new variant!
PowerWyrm is offline   Reply With Quote
Old August 1, 2012, 16:24   #2
Derakon's Avatar
Join Date: Dec 2009
Posts: 9,024
Derakon is on a distinguished road
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.
Derakon is offline   Reply With Quote
Old August 1, 2012, 22:07   #3
Patashu's Avatar
Join Date: Jan 2008
Posts: 526
Patashu is on a distinguished road
Won't good compilers inline the kinds of functions you'd make macros in the first place?
Patashu is offline   Reply With Quote
Old August 1, 2012, 22:47   #4
Angband Devteam member
Join Date: May 2007
Location: London, UK
Posts: 5,057
Magnate is on a distinguished road
Send a message via MSN to Magnate Send a message via Yahoo to Magnate
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.4 is much better than 3.1, 3.2 or 3.3. It still is easier than 3.0.9, but it is more convenient to play without being ridiculously easy, so it is my new favorite of the versions." - Timo Pietila
Magnate is offline   Reply With Quote

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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

All times are GMT +1. The time now is 16:29.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.