Angband.oook.cz
Angband.oook.cz
AboutDownloadVariantsLadderForumCompetitionSpoilersComicScreenshotsFunniesLinks

Go Back   Angband Forums > Angband > Vanilla

Reply
 
Thread Tools Display Modes
Old August 1, 2012, 16:40   #1
PowerWyrm
Knight
 
PowerWyrm's Avatar
 
Join Date: Apr 2008
Posts: 753
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:

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

Code:
#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:

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!
PowerWyrm is offline   Reply With Quote
Old August 1, 2012, 17:24   #2
Derakon
Prophet
 
Derakon's Avatar
 
Join Date: Dec 2009
Posts: 5,974
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, 23:07   #3
Patashu
Swordsman
 
Patashu's Avatar
 
Join Date: Jan 2008
Posts: 378
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, 23:47   #4
Magnate
Angband Devteam member
 
Join Date: May 2007
Location: London, UK
Posts: 5,003
Magnate is on a distinguished road
Send a message via MSN to Magnate Send a message via Yahoo to Magnate Send a message via Skype™ 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
Reply


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 08:05
Macro Problem Larvitz Vanilla 6 January 21, 2009 21:59
Macro Problem? cerberus Vanilla 3 October 8, 2008 10:45
Macro problem Zero Vanilla 2 June 20, 2008 15:52
Macro problem shevek Vanilla 9 May 19, 2008 21:43


All times are GMT +1. The time now is 00:39.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.