Just for the sake of thoroughness, there is another
problem with using macros instead of functions.  Even
if you have everything properly parenthesized, what
happens to this:

        int n, m;

        n = -3;
        m = Abs(n++);

If Abs() were a function, m would be 3 and n would be -2.
With the macro

> #define Abs(a) (((a) >= 0) ? (a) : -(a))

the statement expands to:

        m = (((n++) >= 0) ? (n++) : -(n++));

Which would leave n at -1 when all is said & done.

The autoincrement & autodecrement operators are the bane
of all macro writers...

-- 
-Richard M. Hartman
[EMAIL PROTECTED]

186,000 mi./sec ... not just a good idea, it's the LAW!


> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, March 17, 1999 12:59 PM
> To: [EMAIL PROTECTED]
> Subject: Re: Abs() macro in sysutil.h is incorrect
> 
> 
> 
> >Actually there should be parens around the first "a" too:
> >      #define Abs(a) (((a) >= 0) ? (a) : -(a))
> 
> Consider this now fixed.
> 
> Until you get headers with this change, I suggest adding
> 
> #undef Abs
> #define Abs(a) (((a) >= 0) ? (a) : -(a))
> 
> 
> >My rule of thumb is to always put parens around EVERY occurence of a
> >macro argument in the expansion.
> 
> This is very good advice.
> 
> 
> Thanks for the report.
> 
> 
> -Roger Flores
> 
> 
> 

Reply via email to