warning: operation on 'zero' may be undefined

Paul Schlie schlie@comcast.net
Fri Oct 1 14:25:00 GMT 2004


For my own edification, it's not clear to me that the instruction sequence:

   int zero = 125;
   zero = (++zero)%3;

is ambiguous in any way, as I was under the impression that the left hand
side of an assignment expression will be assigned the resulting value of the
evaluation of it's right hand side, which unambiguously logically equivalent
to:

  int zero = 125;
  zero = ((zero = zero + 1) % 3) ;

Where the compiler would hopefully recognize the opportunity to eliminate
the unnecessary intermediate assignment of zero = zero + 1, as zero is not
declared as being volatile, nor implied due to a lack of a sequence point.

What am I missing?

Thanks, -paul-

On Friday 01 October 2004 03:57, Mathieu Malaterre wrote:
>  I am trying gcc4, and turning -Wall reveal a strange warning:
>
> int main()
> {
>    int zero = 125;
>    zero = (++zero)%3;
> }
>
> g++ -Wall foo.cxx
>
> foo.cxx: In function `int main()':
> foo.cxx:4: warning: operation on 'zero' may be undefined
>
> What does this mean ?

Paul Brook wrote: 

> You incrementing and assigning to the same variable in one statement, which
> invokes undefined behaviour.

> The code you wrote can be interpreted as
> zero = zero%3;
> or
> zero = (zero % 3) + 1
> or possibly even something entirely different.

Dave Korn wrote:

> I'm nitpicking, but considering it's a pre-increment, I'd have said
>
> zero = (zero + 1) % 3;
> or
> zero = zero + 1;
> 
> were the interpretations.  And the problem is the lack of a sequence point
> between them.

Giovanni Bajo wrote:
> The error message could use a clarification though.



More information about the Gcc mailing list