This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: min/max macros


On Thu, 11 Dec 1997, Bill Ahlbrandt wrote:
> I "coded" my own and used them as follows:
> 
> #define max(a,b)	(((a) > (b)) ? (a) : (b))
> #define min(a,b)	(((a) < (b)) ? (a) : (b))

That is a big no-no: Absolutely no performance benefits with any decent
compiler but nasty side effects.

> After discovering that max in particular was not yielding the desired
> results [...]

You didn't happen to use "max(i++,prev_max)" or similar, did you?

For that expands to 
  (((i++) > (prev_max)) ? (i++) : (prev_max))
which may increment i twice as an -- usually unintended -- side-effect.

Hope this helps,
Gerald
-- 
Gerald Pfeifer (Jerry)      Vienna University of Technology
pfeifer@dbai.tuwien.ac.at   http://www.dbai.tuwien.ac.at/~pfeifer/



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]