min/max macros

Gerald Pfeifer pfeifer@dbai.tuwien.ac.at
Thu Dec 11 13:43:00 GMT 1997


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/




More information about the Gcc mailing list