to bug or not to bug

Dave Hooper dave@beermex.com
Mon Jan 13 05:17:00 GMT 2003


> #define max(a,b) (a > b) ? a : b
> cChar = max(1,foo(faArray[iCount++]));
>
> /* INCORRECT output is obtained:
>     output: > ii = 0  ->  ii = 1  ->  iCount=2 */

This is not a gcc bug.  It is a bug in your definition of max.  Think
about what the #define is doing.  When the preprocessor expands max it
generates the following code:

  cChar = (1 > foo(faArray[iCount++])) ? 1 : foo(faArray[iCount++];

You can see that foo gets called twice.

The 'bug' is in your definition of max.  One solution would be to not use
a #define for this sort of thing.  If you must, use an inline function.

d



More information about the Gcc-bugs mailing list