This is the mail archive of the gcc-bugs@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]
Other format: [Raw text]

Re: to bug or not to bug


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


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